如果不进行一些更改,就不能使用该堆栈将任何CSS导入到您的供应商文件中。
为什么?因为这行代码:
import 'bootstrap/dist/css/bootstrap.css';
它只是将你的CSS作为字符串导入,而实际上你想要的是在样式标签中放置供应商的CSS。 如果您检查config/webpack.commons.js
,您会找到此规则:
{
test: /\.css$/,
loaders: ['to-string-loader', 'css-loader']
},
这个规则允许您的组件导入css文件,基本上就是这样:
@Component({
selector: 'app',
encapsulation: ViewEncapsulation.None,
styleUrls: [
'./app.component.css'
],
在AppComponent中没有封装,因为这行代码encapsulation: ViewEncapsulation.None,
的存在,这意味着任何CSS规则都将全局应用于您的应用程序。因此,您可以在应用程序组件中导入Bootstrap样式:
@Component({
selector: 'app',
encapsulation: ViewEncapsulation.None,
styleUrls: [
'./app.component.css',
'../../node_modules/bootstrap/dist/css/bootstrap.css'
],
但如果您坚持要将其导入到vendor.ts
中,则需要安装一个新的加载器,npm i style-loader --save-dev
,这将允许webpack将CSS注入到您的页面中。然后,您需要创建一个特定的规则,在您的webpack.common.js
文件中更改现有的规则:
{
test: /\.css$/,
loaders: ['style-loader', 'css-loader'],
include: [/node_modules/]
},
{
test: /\.css$/,
loaders: ['to-string-loader', 'css-loader'],
exclude: [/node_modules/]
},
只有在从node_modules
中导入CSS时才会应用第一条规则,而从node_modules
以外导入CSS时将应用第二条规则。