我正在构建一个 angular2 应用程序。当我尝试为同一个组件加载多个样式表时,我遇到了多个问题。这是我正在做的代码:
import { Component } from '@angular/core';
@Component({
selector: 'my-tag',
templateUrl: 'my-tag.component.html',
styleUrls: [
'./style1.css',
'./style2.css'
]
})
export class MyTagComponent{}
现在这是我的问题:
当我尝试从其他目录中包含 css 文件时,如下所示:
styleUrls: [
'./style1.css',
'../public/style2.css'
]
我得到错误
Uncaught Error: Expected 'styles' to be an array of strings.
在浏览器控制台中。
然后我将第二个样式表 style2.css
包含在组件的目录中并编写了第一个片段。现在正在加载样式,但正在全局加载。第二个样式表的类名与引导程序冲突,而不是引导程序的样式,第二个样式表的样式被全局应用,即其他组件的模板从第二个样式表进行样式设置。
styleUrls
中列出的url不应该只应用于组件而不会对其他组件造成伤害吗?
谁能告诉我如何在不全局应用的情况下为特定组件加载多个 css 文件。
我也尝试了以下解决方法,但该样式已应用于我制作的所有组件。
styles: [
require('./style1.css').toString(),
require('../public/style2.css').toString()
]
PS 我正在使用 webpack 作为我的项目的模块捆绑器。
webpack.config(摘录)
{
test: /\.css$/,
exclude: helpers.root('src', 'app'),
loader: ExtractTextPlugin.extract('style', 'css?sourceMap')
},
{
test: /\.css$/,
include: helpers.root('src', 'app'),
loader: 'raw'
}
原文由 kanra-san 发布,翻译遵循 CC BY-SA 4.0 许可协议
我已经看到使用这种方法:
其中 app.component.css 有多个导入,因此:
我希望这有帮助。