简体   繁体   English

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

[英]How to change the background colour of different components in Angular2?

In an Angular2 app, there is a problem I keep encountering, ie, setting the background colour or image of a page.在 Angular2 应用程序中,我一直遇到一个问题,即设置页面的背景颜色或图像。 In the app, if I mention a particular colour in the styles.css for the background then that colour is applied for all the pages I develop, since it applies the styles globally.在应用程序中,如果我在 style.css 中为背景提及特定颜色,那么该颜色将应用于我开发的所有页面,因为它会全局应用这些样式。 Now If my login page is blue in colour and I want to change the background colour of my home page to white, how do I go about doing that?现在,如果我的登录页面是蓝色的,而我想将主页的背景颜色更改为白色,我该怎么做? Because in the component Homepage we have : homepage.component.html and homepage.component.css .因为在组件 Homepage 中有: homepage.component.html 和 homepage.component.css 。 In homepage.component.css, the css only affects the elements of the homepage.在 homepage.component.css 中,css 只影响主页的元素。 I cannot change the colour of the entire page or add an image as a background for the entire page either since body { ... } will not work there.我无法更改整个页面的颜色或添加图像作为整个页面的背景,因为body { ... }在那里不起作用。 Neither does @import url work. @import url 也不起作用。

How can I change the background colours of different components in an Angular2 app?如何更改 Angular2 应用程序中不同组件的背景颜色?

Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks.谢谢。

Here is an other solution.这是另一个解决方案。 It is maybe a little basic,restrictif or hacky, but it works:它可能有点基本,restrictif 或 hacky,但它有效:

style.css样式文件

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

.my-container{
  width: 100vw;
  height: 100vh;
  ...
}

home.component.css主页.component.css

.my-container{
    background-color: red;
 }

home.component.html主页.component.html

<div class="my-container">
   ...
   /* the content of your component here */
</div>

login.component.css登录名.component.css

.my-container{
    background-color: blue;
 }

login.component.html登录.component.html

<div class="my-container">
   ...
   /* the content of your component here */
</div>

and so on.等等。

You can use the :host selector to apply styles in the host component's template, the one that is parent to the current component.您可以使用:host选择器在宿主组件的模板中应用样式,该模板是当前组件的父组件。 In your component's css, try this:在组件的 css 中,试试这个:

:host .my-app-class {
    background-color: white;
}

this is my solution.这是我的解决方案。

style.css样式文件

.login-page {
    height: 100%;
      background-repeat: no-repeat;
      background-image: linear-gradient(rgb(104, 145, 162), rgb(12, 97, 33));
}

login-home.component.ts登录-home.component.ts

keep the eye in the OnInit and OnDestroy functions注意OnInitOnDestroy函数

import { Component, OnInit, OnDestroy } from '@angular/core';

@Component({
  selector: 'app-login-home',
  templateUrl: './login-home.component.html',
  styleUrls: ['./login-home.component.css']
})
export class LoginHomeComponent implements OnInit, OnDestroy {
  // Search the tags in the DOM
  bodyTag: HTMLBodyElement = document.getElementsByTagName('body')[0];
  htmlTag: HTMLElement = document.getElementsByTagName('html')[0];

  constructor() {

  }

  ngOnInit() {
    // add the css-style to the html and body tags
    this.bodyTag.classList.add('login-page');
    this.htmlTag.classList.add('login-page');
  }

  ngOnDestroy() {
    // remove the the body classes
    this.bodyTag.classList.remove('login-page');
    this.htmlTag.classList.remove('login-page');
  }

}
import { ViewEncapsulation } from '@angular/core';

@Component({
  encapsulation: ViewEncapsulation.None,

})

To override the styles in the styles.css and specify specific styes for the compoenent add ViewEncapsulation as none into your component and import the right class, it will work.要覆盖styles.css 中的样式并为组件指定特定样式,请将ViewEncapsulation 作为none添加到您的组件中并导入正确的类,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM