styleUrls-无法使用角度2中的相对路径为组件加载CSS样式表

问题描述:

我在Angular 2的"app/Hero"下定义了一个"Hero"组件.hero组件定义为"hero.component.html","hero.component.css"和"hero.component.ts"文件.templateUrl可以正常工作,但不会加载styleUrls.将显示资源未找到错误.我引用了此链接 https://angular.io/docs/ts/latest/guide/component-styles.html#!#relative-urls 使用相对URL加载样式.

I have a "Hero" component defined under "app/Hero" in angular 2. The hero component as "hero.component.html","hero.component.css" and "hero.component.ts" files defined. the templateUrl works fine but the styleUrls do not load. Resource not found error will be displayed. I referred this link https://angular.io/docs/ts/latest/guide/component-styles.html#!#relative-urls to load styles using relative URLs.

  @Component({
  providers: [HeroService],
  templateUrl: './Hero.component.html',
  styleUrls: ['./Hero.component.css'],
})

这对我有用:

styles: [require('./app.component.css').toString()]

并且您必须更改webpack.config.js.代替"raw-loader",您必须指定下一个:

And you have to change webpack.config.js. Instead of 'raw-loader' you have to specify the next:

loader: 'style-loader!css-loader'