[英]Angular error when applying css style: Refused to apply style from 'path' because its MIME type ('text/html') is not a supported stylesheet MIME type
I'm trying to create a function that changes the whole page css, injecting a new styleSheet.我正在尝试创建一个更改整个页面 css 的函数,注入一个新的 styleSheet。
I have the following function:我有以下功能:
loadCSS() {
let fileRef;
fileRef = document.createElement('link');
fileRef.setAttribute('rel', 'stylesheet');
fileRef.setAttribute('type', 'text/css');
fileRef.setAttribute('href', './appointment2.component.scss');
if (typeof fileRef !== 'undefined') {
document.getElementsByTagName('head')[0].appendChild(fileRef);
}
}
Once user click on this function, the new css style should be applied.用户单击此功能后,应应用新的 css 样式。 But it does not happen.
但它不会发生。
I'm getting the following error: Refused to apply style from 'http://localhost:4200/appointment2.component.scss' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
我收到以下错误:
Refused to apply style from 'http://localhost:4200/appointment2.component.scss' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
I've already added the path to my @Component styleUrls
:我已经将路径添加到我的 @Component
styleUrls
:
styleUrls: [
'./appointment2.component.scss',
'./appointment.component.scss'],
I've even already added it to my styles
at angular.json
:我什至已经在
angular.json
将它添加到我的styles
中:
"styles": [
"src/app/appointment/appointment2.component.scss",
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.scss",
"src/theme.scss",
"node_modules/ngx-toastr/toastr.css"
]
What am I doing wrong?我究竟做错了什么? How to fix this?
如何解决这个问题?
SASS (.scss) files are not valid CSS files. SASS (.scss) 文件不是有效的 CSS 文件。
They need to be transpiled into CSS first.它们需要首先被转译成 CSS。
This means you can't use them in directly in your application.这意味着您不能直接在您的应用程序中使用它们。
Try providing a CSS (.css) file instead.请尝试提供 CSS (.css) 文件。
With the great help of @temp_user I discovered the file needed to be a '.CSS' file, and not '.SCSS'.在@temp_user的大力帮助下,我发现该文件需要是“.CSS”文件,而不是“.SCSS”。 But I was still getting one problem: The file could not be found (404 error at payload).
但我仍然遇到一个问题:找不到文件(有效负载出现 404 错误)。 So what I needed to do (besides changing the file format) was Move the file to
/assets
directory, and after this.所以我需要做的(除了更改文件格式)是将文件移动到
/assets
目录,然后再移动。 Everything worked fine.一切正常。
So, my code ended like this:所以,我的代码是这样结束的:
loadCSS() {
let fileRef;
fileRef = document.createElement('link');
fileRef.setAttribute('rel', 'stylesheet');
fileRef.setAttribute('type', 'text/css');
fileRef.setAttribute('href', '../../assets/dynamicStyles/appointment2.component.css');
if (typeof fileRef !== 'undefined') {
document.getElementsByTagName('head')[0].appendChild(fileRef);
}
}
With this code, I'm able to change my page full style, anytime I need.使用此代码,我可以随时更改我的页面完整样式。
NOTE It is not necessary to add the file path in "styles" inside angular.json
, nor in @Component inside my app.component.ts
注意不必在
angular.json
中的“样式”中添加文件路径,也不必在我的app.component.ts
中的 @Component 中添加文件路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.