简体   繁体   English

如何在 Angular 中更改整个页面的背景颜色

[英]How to change whole page background-color in Angular

I am trying to change the background color for the whole page in Angular(would use body, or html tag when working without framework).我正在尝试更改 Angular 中整个页面的背景颜色(在没有框架的情况下会使用 body 或 html 标签)。 and I can't find the best practice for this, I want it to be within the framework if possible since I am building an application for years to come.我找不到这方面的最佳实践,我希望它尽可能在框架内,因为我正在构建一个未来几年的应用程序。

If you are using angular-cli.如果您使用的是 angular-cli。
There should exist a global style file.应该存在一个全局样式文件。

  • src源文件
    • app应用程序
    • assets资产
    • environments环境
    • index.html索引.html
    • styles.css样式文件

In there you should be able to put your style eg html { background-color: black; }在那里你应该能够把你的风格,例如html { background-color: black; } html { background-color: black; } to effect the whole page. html { background-color: black; }来影响整个页面。

You can do this from any of your component.您可以从任何组件执行此操作。 For example:例如:

export class AppComponent implements AfterViewInit {
    constructor(private elementRef: ElementRef) {}
    ngAfterViewInit() {
        this.elementRef.nativeElement.ownerDocument
            .body.style.backgroundColor = 'yourColor';
    }
}

By using this.elementRef.nativeElement.ownerDocument , you can access the window.document object without violating any angular convention.通过使用this.elementRef.nativeElement.ownerDocument ,您可以在不违反任何角度约定的情况下访问window.document对象。 Of course, you can directly access the document object using window.document but, I think it would be better to access it through ElementRef .当然,您可以使用window.document直接访问document对象,但我认为通过ElementRef访问它会更好。

Generally speaking, using ElementRef could make your app more vulnerable to XSS attacks.一般来说,使用 ElementRef 会使您的应用更容易受到 XSS 攻击。 Refer to this official Angular doc for more information.有关更多信息,请参阅此官方 Angular 文档

Also, setting a global style in your styles.css file, as Andresson has suggested, might not solve your problem if you are working with multiple components which have their own Shadow DOMs .此外,如 Andresson 所建议的那样,在你的 style.css 文件中设置一个全局样式可能无法解决你的问题,如果你正在使用多个具有自己Shadow DOM 的组件。

Here's a safer solution to your problem, using View Encapsulation .这是使用View Encapsulation解决您的问题的更安全的解决方案。
Set View Encapsulation to None (don't forget to import) in your app.component.ts :app.component.ts 中将View Encapsulation 设置为 None(不要忘记导入):

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  encapsulation: ViewEncapsulation.None
})

Next, go to your app.component.css file and simply add your background color:接下来,转到您的app.component.css文件并简单地添加您的背景颜色:

body {
  background-color: green;
}

This change will affect your app globally.此更改将在全球范围内影响您的应用程序。Read more about it here .在此处阅读更多相关信息

不知道为什么没有提到它,但是将它添加到全局 css 文件中效果很好:

body { background-color: red !important; }

The following solution is another alternative that I came across while I was facing the same problem.以下解决方案是我遇到相同问题时遇到的另一种选择。 For the application to be more adaptable on different platforms we can use Renderer2 class provided by Angular as a service like this:为了使应用程序在不同平台上更具适应性,我们可以使用 Angular 提供的Renderer2类作为这样的服务:

export class AppComponent {

constructor(private el: ElementRef, private renderer:Renderer2){}

  ngAfterViewInit(){

this.renderer.setStyle(this.el.nativeElement.ownerDocument.body,'backgroundColor', 'yourchoice color');

}

}

More details regarding Renderer2 and its methods can be found here: Render2_Description可以在此处找到有关 Renderer2 及其方法的更多详细信息: Render2_Description

You may also want to reconsider the font color:您可能还想重新考虑字体颜色:

body
{
  background-color: black !important;
  color:greenyellow;
}
h1
{
  color: aquamarine;

you can set whole content in one div and set style for that div:您可以在一个 div 中设置全部内容并为该 div 设置样式:

<div  style=" background-color: black; height: 100vh;">

height:100vh will set height to full length of your screen so that the background color will be applied like setting background for whole html but just for that page. height:100vh 会将高度设置为屏幕的全长,以便应用背景颜色,就像为整个 html 设置背景一样,但只是针对该页面。

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

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