【发布时间】:2017-04-07 09:06:33
【问题描述】:
我正在构建一个 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 包含在组件的目录中,并编写了第一个sn-p。现在正在加载样式,但正在全局加载。第二个样式表的类名与引导程序冲突,而不是引导程序的样式,第二个样式表的样式被全局应用,即其他组件的模板正在从第二个样式表设置样式。
styleUrls中列出的url不应该只应用在组件上,而不会对其他组件造成伤害吗?
谁能告诉我如何在不全局应用的情况下为特定组件加载多个 css 文件。
我也尝试了以下解决方法,但该样式已应用于我制作的所有组件。
styles: [
require('./style1.css').toString(),
require('../public/style2.css').toString()
]
附:我正在使用 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'
}
【问题讨论】:
标签: css twitter-bootstrap angular webpack styles