Mime type error when adding a CSS file to Angular

后端 未结 9 834
野的像风
野的像风 2020-12-20 11:36

I\'m trying to build a static site using Angular. What I want is to have some global css/js/image files to be added to the index.html

This is my code in

9条回答
  •  遥遥无期
    2020-12-20 11:42

    For someone who will face same problem.

    The usual reason for this error message is when the browser tries to load that resource, the server returns an HTML page instead. For example, if your router catches unknown paths and displays a default page without a 404 error. Of course that means the path does not return the expected CSS file / image / icon / whatever... ref from here

    Lets assume we have Angular 6 project with a file structure like this:

    project
        |-src
          |-app
          |-assets
          |-environments 
          |----
    

    lets assume we need to put a theming folder (that contains css files) directly inside src folder.

    project
        |-src
          |-app
          |-assets
          |-environments
          |-vendor // Theming
             |-theme-light.css
              |theme-dark.css
    

    if we tried to access that theme file like this:

    
    

    an error will be thrown.

    Refused to apply style from 'http://localhost:4200/vendor/theme-dark.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

    If we paste that url in the browser, it will not give the plain css file because the path is wrong.

    Solution

    So What you need to do is find the correct path. (we can find out by past that url in the browse and see what will returned)

    In whis case putting ../src/ reference will fix the error

    
    

    Note: The exact path and router configuration depends on how you have setup your project and the libraries you are using with Angular.

提交回复
热议问题