【问题标题】:Imported Styles are not applied to components导入的样式不适用于组件
【发布时间】:2018-11-08 15:38:13
【问题描述】:

我想创建单独的文件来存储 IE 9-11 的样式。为此,我创建了文件 InternetExplorer.scss 并将其导入 主文件styles.scss:

@import "scss/InternetExplorer.scss";

InternetExplorer.scss 有一种风格:

@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
    .nav-tabs .nav-item {
        margin-bottom: -1px;
        z-index: 0;
    }
}    

但是,声明的样式不会应用于页面。

但是,如果我在组件的样式属性中声明此样式,则可以完美应用 IE 样式:

@Component({
    selector: 'tabComponent',    
    templateUrl: 'tabComponent.module.html',
    styles:
        `@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
            .nav-tabs .nav-item {
                margin-bottom: -1px;
                z-index: 0;
            }
         }`,
    encapsulation: ViewEncapsulation.None

样式的位置是:

app/styles.scss
app/scss/InternetExplorer.scss

tabComponent的位置是:

app/coreComponents/tabComponent

如果我写两个点@import "../scss/InternetExplorer.scss";,那么File to import not found or unreadable: ../scss/InternetExplorer.scss.

如果我写一个点@import "./scss/InternetExplorer.scss";,那么样式就不会应用。

Angular 版本是 6。

如何应用来自InternetExplorer.scss的样式?

【问题讨论】:

  • scss 文件夹的路径是什么?
  • @MohamedAliRACHID 我已经添加了问题的路径。
  • 我更新了答案,请检查

标签: css angular sass


【解决方案1】:

来自 Angular 文档:

/* AOT 编译器需要./ 来表明这是本地的 */

由于您的 scss 文件夹不在本地,请尝试将 ./ 添加到路径:

@import "./scss/InternetExplorer.scss";

【讨论】:

  • 如果我写@import "../scss/InternetExplorer.scss";,那么File to import not found or unreadable: ../scss/InternetExplorer.scss. 如果我写一个点@import "./scss/InternetExplorer.scss";,那么样式就不会被应用。
  • 你能指定组件的路径吗?
  • 我已将组件的路径添加到问题中。 tabComponent 的位置是app/coreComponents/tabComponent
  • 谢谢,它有效。有必要再次运行npm-start。非常感谢!
猜你喜欢
  • 2016-07-11
  • 2016-12-12
  • 2019-06-13
  • 1970-01-01
  • 2021-01-23
  • 2020-10-19
  • 2020-04-28
  • 2021-12-20
  • 2017-04-15
相关资源
最近更新 更多