简体   繁体   English

应用 css 样式时出现角度错误:拒绝从“路径”应用样式,因为其 MIME 类型(“文本/html”)不是受支持的样式表 MIME 类型

[英]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.

相关问题 拒绝应用来自“xxx/style.css”的样式,因为它的 MIME 类型(“text/html”)不是受支持的样式表 MIME 类型 - Refused to apply style from 'xxx/style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type 拒绝应用{filename}中的样式,因为它的MIME类型(&#39;text / html&#39;)不是受支持的样式表MIME类型 - Refused to apply style from {filename} because its MIME type ('text/html') is not a supported stylesheet MIME type Django:拒绝从...应用样式,因为它的 MIME 类型(&#39;text/html&#39;)不是受支持的样式表 MIME 类型 - Django: Refused to apply style from ... because its MIME type ('text/html') is not a supported stylesheet MIME type 拒绝应用“ <URL> &#39;,因为它的MIME类型(&#39;text / html&#39;)不是受支持的样式表MIME类型,并且启用了严格的MIME检查 - Refused to apply style from '<URL>' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled 拒绝应用样式,因为它的 MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型 - Refused to apply style because its MIME type ('text/html') is not a supported stylesheet MIME type 拒绝应用来自 'http://localhost:3000/css/style.css' 的样式,因为它的 MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型 - Refused to apply style from 'http://localhost:3000/css/style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type 拒绝应用样式,因为它的 MIME 类型('text/html')不是受支持的样式表类型 - Refused to apply style because its MIME type ('text/html') is not a supported stylesheet type 拒绝应用来自 'http://localhost:3000/assets/styles/signup.css' 的样式,因为它的 MIME 类型('text/html')不是受支持的样式表 MIME 类型 - Refused to apply style from 'http://localhost:3000/assets/styles/signup.css' because its MIME type('text/html')is not a supported stylesheet MIME type 拒绝从...应用样式,因为它的 MIME 类型 (&#39;text/html&#39;) 不是受支持的样式表 MIME 类型,并且启用了严格的 MIME 检查 - Refused to apply style from... because its MIME type ('text/html') is not a supported style-sheet MIME type, and strict MIME checking is enabled ionic/angular2 - 拒绝应用来自 'http://localhost:8100/build/main.css' 的样式,因为它的 MIME 类型('text/html')不受支持 - ionic/angular2 - Refused to apply style from 'http://localhost:8100/build/main.css' because its MIME type ('text/html') is not a supported
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM