首页 文章

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

提问于
浏览
1

我在"app/Hero"中以角度2定义了一个"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加载样式 .

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

1 回答

  • 0

    检查文件名并确保正确输入 . 它可能区分大小写!还要确保遵循正确的文件夹结构 .

    如果组件声明中包含以下内容

    @Component({
      selector: 'app-hero',
      templateUrl: './hero.component.html',
      styleUrls: ['./hero.component.css'],
    })
    

    您的文件夹层次结构应如下所示:

    app.module.ts
    hero
    - hero.component.ts
    - hero.component.css
    - hero.component.html
    

相关问题