简体   繁体   English

如何为Angular组件设置背景图像?

[英]How to set background image for Angular component?

In my angular app I would like to set a background image for a specific view. 在我的角度应用程序中,我想为特定视图设置背景图像。

To this end, I added the following to the css-file of a component: 为此,我将以下内容添加到组件的css文件中:

body {
       background-image: url("../../../assets/images/backgroundImage.jpg");
   }

However, the background doesn't change. 但是,背景不会改变。

This the the file path of the file containing the css-code shown above: 这是包含上面显示的css代码的文件的文件路径:

angular-app/src/app/users/profile/profile.component.css angular-app / src / app / users / profile / profile.component.css

... and this is the file path of the background-image: ...这是背景图片的文件路径:

angular-app/src/assets/images/backgroundImage.jpg angular-app / src / assets / images / backgroundImage.jpg


I also tried 我也试过

body {
       background-image: url("assets/images/backgroundImage.jpg");
   }

... but this resulted in a warning during compilation and didn't work either. ...但这在编译过程中导致警告,也没有起作用。

What am I doing wrong? 我究竟做错了什么?


I gave the root element class "root" and then put the following into the css-file: 我给了根元素类“ root”,然后将以下内容放入css文件中:

.root {
   background-image: url("../../../assets/images/backgroundImage.jpg");
}

... now the background changes but not for the whole vertical length of the screen (the lower part remains white): ...现在背景改变了,但屏幕的整个垂直长度却没有改变(下部保持白色):

在此处输入图片说明

According to Angular , The styles specified in @Component metadata apply only within the template of that component. 根据Angular@Component Component元数据中指定的样式仅适用于该组件的模板内。

you can use a hack like this 你可以像这样使用hack

In your styless.css file add this 在您的styless.css文件中添加

.selector {
       background-image: url("../../../assets/images/backgroundImage.jpg");
   }

now in your component you can do this 现在在您的组件中,您可以执行此操作

ngOnInit() {
    document.body.className = "selector";
  }

ngOnDestroy(){
    document.body.className="";
  }

But this is highly not recommended, i dont know what your code looks like, but there must be another way. 但是强烈不建议这样做,我不知道您的代码是什么样子,但是必须有另一种方法。

  1. Scale your component to fit whole view-port 缩放组件以适合整个视口
  2. set the background on your component 在您的组件上设置背景

I will work on a plunker and link to this file as an edit when done 我将使用插件,并在完成后链接到该文件作为编辑

I will add another answer for this because its totaly different from my previous answer 我将为此添加另一个答案,因为它与我之前的答案完全不同

in your component import ViewEncapsulation from angular/core 在组件中从angular/core导入ViewEncapsulation

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

In your @component metatag add encapsulation: ViewEncapsulation.None, 在@component元标记中,添加encapsulation: ViewEncapsulation.None,

@Component({
  ...
  encapsulation: ViewEncapsulation.None,
  ...
})

This has a side effect though, all styles in your component will be available to all other components once it loads. 但是,这样做有副作用,一旦加载,组件中的所有样式将对所有其他组件可用。

You can check more about it on the Angular page 您可以在“ Angular”页面上查看有关它的更多信息

您可能需要创建服务或使用ngrx在子组件和app.component之间进行通信,以使用ngClass切换app.component.html的样式。

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

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