如何在Angular2中更改不同组件的背景颜色?

Ste*_*son 2 html css angular

在Angular2应用程序中,我遇到了一个问题,即设置页面的背景颜色或图像.在应用程序中,如果我在styles.css中提到背景中的特定颜色,那么该颜色将应用于我开发的所有页面,因为它全局应用样式.现在如果我的登录页面是蓝色的,并且我想将我的主页的背景颜色更改为白色,我该如何去做呢?因为在组件主页中我们有:homepage.component.html和homepage.component.css.在homepage.component.css中,css仅影响主页的元素.我无法更改整个页面的颜色或添加图像作为整个页面的背景,因为它 body { ... }不会在那里工作.@import url也不起作用.

如何更改Angular2应用程序中不同组件的背景颜色?

任何帮助将不胜感激.谢谢.

Veg*_*ega 6

另一种解决方案,有点基础,但有效:

style.css文件

 body, html {
   padding: 0;
   width: 100vw;
   height: 100vh;
 }

.my-container{
  width: 100vw;
  height: 100vh;
  ....
}
Run Code Online (Sandbox Code Playgroud)

home.component.css

.my-container{
    background-color: red;
 }
Run Code Online (Sandbox Code Playgroud)

home.component.html

<div class="my-container>
   ...
   /* the content of your component here */
</div
Run Code Online (Sandbox Code Playgroud)

login.component.css

.my-container{
    background-color: blue;
 }
Run Code Online (Sandbox Code Playgroud)

login.component.html

<div class="my-container>
   ...
   /* the content of your component here */
</div>
Run Code Online (Sandbox Code Playgroud)

等等.

  • 我曾经尝试过这个。但随后边距就成了问题。div 周围有这些空白。 (2认同)

Fai*_*sal 2

您可以使用:host选择器应用宿主组件\xe2\x80\x99s 模板(当前组件的父组件)中的样式。在组件的 css 中,尝试以下操作:

\n\n
:host .my-app-class {\n    background-color: white;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

  • 没有帮助。我把它保存在app.component.css中,在那里不起作用,所以我把它保存在dashboard.component.css中,它在那里也不起作用。styles.css 中的内容为准。 (2认同)