首页 > 解决方案 > 在 index.html 的顶部添加了捆绑的 css

问题描述

在构建我的 Angular 9.0.4 应用程序的 prod 版本时,css 被捆绑并添加到 dist/index.html 的顶部,如下所示:

<link rel="stylesheet" href="styles.6ea28d52542acb20a4c6.css"><!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>MyTitle</title>
  <base href="/">

我认为这导致了 Chrome 中的警告:

Resource interpreted as Stylesheet but transferred with MIME type text/html:

并且样式表未正确呈现。请注意,仅在加载带有子文件夹的域时才会发生这种情况,例如www.mydomain.com/product/1

是否可以配置构建,以便在下面添加样式表元素<base href="/">,这可能会修复警告?

标签: cssangular

解决方案


我不知道有任何方法可以移动 css 文件导入,但您可以通过关闭 css 提取来停止此错误:

纱线构建--extractCss=false

或者在 angular.json 中的构建配置中

{
 ...
 "build": {
  "configurations": {
    "yourBuildName": {
      ...
      "extractCss": false
    }
  }
 }
}

推荐阅读