css - Angular如何从服务器加载css
问题描述
我想尝试从服务器/外部源介绍角度加载 css。我已经在尝试使用 DomSanitizer 但没有成功。样式表显示在 chrome 的网络选项卡内,但 html 不应用样式表。
在 TS 文件中:
import {Component, OnInit, ViewEncapsulation} from '@angular/core';
import {DomSanitizer} from "@angular/platform-browser";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
encapsulation: ViewEncapsulation.None,
})
export class AppComponent implements OnInit{
title = 'angulardynamicss';
cssUrl = 'http://localhost:8080/api/csspoc/asFile'
constructor(public sanitizer: DomSanitizer) {}
ngOnInit(): void {}
}
HTML 看起来像:
<link rel="stylesheet" [href]='sanitizer.bypassSecurityTrustResourceUrl("http://localhost:8080/api/csspoc/asFile")'>
<p class="test">hello</p>
样式表文件包含:
.test {
background-color: blue;
}
p{
color:yellow;
}
解决方案
为什么要在 AppComponent 中注入呢?你不能只在你的 index.html中添加<link>
标签吗?<head>
推荐阅读
- flutter - 我如何从标题中获取令牌?
- python - Python pymysql : cannot run multiple statements
- mule - Mule exception strategy
- excel - XIRR function with extremely low ending value
- r - dplyr new version 1.0.0 eliminate tbl_cube function and I found no replace of it
- python - WTForms Regexp validator: How to match white space within html tags using regex
- bash - Concatenate multiple files based on similar ID in a loop
- security - Delphi - 使用 LOGONUSER 针对 AD 组对用户进行身份验证
- android - 如何在一个片段中添加图像并将其显示在另一个片段中/通过android中的捆绑传递图像
- reactjs - 使用“Navigate(-1)”无法返回 Gatsby 中的先前位置