3

我已经用一个新项目对此进行了测试,没有什么独特之处。

ng new debug-project

然后我在导致这个的头部部分添加了一些标签。

<!doctype html>
<html lang="en">

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

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="viewport" content="user-scalable=no, width=device-width" />
  <meta name="viewport" content="width=device-width" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
  <meta name="viewport" content="width=device-width; initial-scale=1; minimum-scale=1; maximum-scale=1; user-scalable=0">

  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>

<body>
  <app-root></app-root>
</body>

</html>

之后ng build --prod,html现在看起来像这样,浏览器无法读取。请注意视口元标记值中缺少的空格。width, initial-scale=1, 成为width,initial-scale=1.

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>DebugProject</title>
    <base href="/">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="viewport" content="user-scalable=no,width=device-width" />
    <meta name="viewport" content="width=device-width" />
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
    <meta name="viewport" content="width=device-width;initial-scale=1;minimum-scale=1;maximum-scale=1;user-scalable=0">
    <link rel="icon" type="image/x-icon" href="favicon.ico">
    <link href="styles.ac89bfdd6de82636b768.bundle.css" rel="stylesheet" />
</head>

<body>
    <app-root></app-root>
    <script type="text/javascript" src="inline.318b50c57b4eba3d437b.bundle.js"></script>
    <script type="text/javascript" src="polyfills.bf95165a1d5098766b92.bundle.js"></script>
    <script type="text/javascript" src="main.8df9530e9d55fa951861.bundle.js"></script>
</body>

</html>

我的设置有问题吗?我可能需要在项目上安装一些东西?

4

3 回答 3

7

从 angular/cli 文档看来,生产构建标志默认会缩小所有 html 文件,包括 index.html https://github.com/angular/angular-cli/issues/1861。您可以通过将元标记包装在忽略标记中来保留元标记中的格式,如下所示:

<!doctype html>
<html lang="en">

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

  <!-- htmlmin:ignore -->
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="viewport" content="user-scalable=no, width=device-width" />
  <meta name="viewport" content="width=device-width" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
  <meta name="viewport" content="width=device-width; initial-scale=1; minimum-scale=1; maximum-scale=1; user-scalable=0">
  <!-- htmlmin:ignore -->

  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>

<body>
  <app-root></app-root>
</body>

</html>
于 2018-03-26T16:09:11.453 回答
2

我会尝试关注这个线程:如何将 Angular 中的 preserveWhitespaces 选项全局设置为 false?

据我所知,问题是在 aot 中构建默认会删除空格。老实说,不构建 aot 并不是一个好主意。它节省了客户端的大量工作并加快了您的 SPA。

于 2018-03-26T16:42:27.097 回答
-2

尝试使用此命令:

ng build -prod --no-aot
于 2018-03-26T11:32:04.367 回答