库中组件的 CSS 文件未随 Angular 应用程序中的组件一起加载

标签 css angular styles angular-cli angular-library

我在 Angular 应用程序(使用 Angular cli 生成)中开发了一个库,该库包含一个具有自己的模板文件和样式表的组件。但是,当我在 app.module.ts 中加载模块时并运行应用程序(为简单起见使用 ng serve),样式表中的样式不会呈现。

  • 库是使用 ng generate library 生成的
  • 库包含在 projects 中应用目录
  • 该组件存在于 <name of the component>/src/lib 中目录

app.module.ts文件包含以下代码:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { GuitarChordGeneratorModule } from 'projects/guitar-chord-generator/src/public-api';

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        GuitarChordGeneratorModule
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }

以下是 guitar-chord-generator.component.ts 的摘录文件:

import { Component, OnInit, Input } from '@angular/core';
import { Chord } from './chord';
import { GCSGConfig } from './gcsgconfig';

@Component({
    selector: 'lib-guitar-chord-generator',
    templateUrl: './guitar-chord-generator.component.html',
    styles: ['./guitar-chord-generator.component.css']
})

下图是应用的目录结构。

enter image description here

如您所见guitar-chord-generator.component.css是组件的 CSS 文件。

最佳答案

你的组件有

styles: ['./guitar-chord-generator.component.css']

The styles property takes an array of strings that contain CSS code.

你应该使用 styleUrls

styleUrls: ['./guitar-chord-generator.component.css']

关于库中组件的 CSS 文件未随 Angular 应用程序中的组件一起加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56499405/

相关文章:

angular - FormArray TypeError : value. forEach 不是函数

html - 使用 CSS 对图像进行平移效果

android - 如何让aapt.exe在SDK Tools r12中解析android样式

jquery - 将特定样式从一个元素复制到另一个元素

jquery .stop() 在每次悬停时降低 div 的高度

angular - 当我点击 ionic 图标时, ionic 卡中的方法也会被触发

html - DIV 高度不是 100%

typescript - 与路由器 socket 的 Angular2 组件通信

html - 并排div不对齐

css - 当应用关键帧动画时变换比例元素