【问题标题】:Angular 2 importing node_modules not adding to buildAngular 2导入node_modules未添加到构建
【发布时间】:2017-11-02 23:43:46
【问题描述】:

我在使用 MS IE 10/11 中运行的某些组件时遇到问题。 根据 Angular.io (https://angular.io/docs/ts/latest/guide/browser-support.html) 的这篇文章,我需要导入一个外部脚本。

<script src="node_modules/core-js/client/shim.min.js"></script>

我放在我的 index.html 中

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <base href="/">
        <link rel="icon" type="image/x-icon" href="/assets/favicon.ico">
        ...
        <script src="../node_modules/core-js/client/shim.min.js"></script>
    </head>
    <body>
        <app-root></app-root>
    </body>
 </html>

但是当我为生产而构建时,它不会将其添加到构建中,它只是将其留在那里,以在运行应用程序时出现脚本未找到错误结束。

我可能遗漏了什么,但我不知道是什么。

提前致谢!

【问题讨论】:

  • Angular 不会读取您的 index.html 文件来确定它需要捆绑哪些依赖项。当您进入构建阶段时,index.html 文件基本上没有受到影响。您必须通过适当的系统通道将此指定为依赖项。但是,执行此操作的细节实际上可能有很大差异,这取决于您的构建是如何管理的。你在使用角度cli吗?您是否下载了 Angular 种子项目(可能使用 webpack 或 systemjs)?基本上,有多种方法可以管理构建过程,答案取决于您的构建是如何管理的。
  • 我正在使用 Angular Cli 和 webpack,这是一个从头开始构建的大型应用程序。

标签: javascript angular build node-modules


【解决方案1】:

在您的.angular-cli.json 文件中,找到(或创建)应用定义中的"scripts" 键,并在shim.js 脚本的数组中添加一个条目,例如:

"scripts": [
  "../node_modules/core-js/client/shim.min.js"
],

重新启动您的 ng serve 或重建您的捆绑包,您应该一切顺利。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    需要在angular-cli.json文件中添加CSS文件和JS文件。如果您在这种情况下不使用 angular-cli,则需要在您正在使用的构建工具中进行配置

    【讨论】:

      猜你喜欢
      • 2021-10-08
      • 2023-01-22
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      相关资源
      最近更新 更多