首页 > 解决方案 > 无法通过页面 css 文件向“body”标签添加值

问题描述

在我的 Angular 应用程序中,我正在向 body 添加一个类名,例如:

ngOnInit() {
    this.store.updatePageClass('page-quoteCart');
  }

在 page.css 我正在写一个类,如:

@media only screen and (min-width: 320px)  and (max-width: 620px) {

    .page-quoteCart{
        border:2px solid red; //but not added
    }

}

但没有得到输出。assets如果我在有效的文件夹中的 style.css 中编写相同的内容。这里有什么问题?

如何根据页面编写css?

标签: sassangular5

解决方案


encapsulation: ViewEncapsulation.None在您的html中添加。它将解决您的问题。

例如,在您更新之后encapsulation: ViewEncapsulation.None- 您的组件文件将如下所示:

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

@Component({
  selector: 'xxxxx',
  templateUrl: 'xxxxx.html',
  styleUrls: ['xxxx.scss'],
  encapsulation: ViewEncapsulation.None
})

推荐阅读