【发布时间】:2019-09-19 21:43:45
【问题描述】:
我想获取一个组件的 CSS 文件内容。
例如:
hello-world.component.ts:
@Component({
selector: 'app-hello-world',
template: `<h1>Hello World</h1>`,
styleUrls: ['./hello-world.component.css']
})
export class HelloWorldComponent {
getCSSContent(){
}
}
hello-world.component.css:
h1 {
color: red;
font-weight: 400;
}
我希望 getCSSContent 函数返回:
h1 {
color: red;
font-weight: 400;
}
【问题讨论】:
-
尝试这样的事情:
this.http.get('./app.component.css').subscribe(data => { console.log('data', data); })(使用 httpClient 的 http 请求) -
您无法在运行时获取 CSS。您无法访问原始 CSS 源代码或找到 Angular 创建的
<script>标签。我所做的是将 CSS 文件复制到我的/assets/文件夹并从那里读取。