本文介绍了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'

这篇关于styleUrls-无法使用角度2中的相对路径为组件加载CSS样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 16:02