【问题标题】:angular cli include/exclude scripts in index.html depending on the environment角度 cli 在 index.html 中包含/排除脚本,具体取决于环境
【发布时间】:2016-12-21 17:11:10
【问题描述】:

我有 angular 2 应用程序(使用了 angular cli)。我想为该应用构建 Web 和移动 (cordova) 版本。

所以我想使用ng build -e prod 为生产构建,ng build -e cordova --output-path mobile/www --base-href ./ 为cordova 项目构建。

如果环境是科尔多瓦,我想包括 <script type="text/javascript" src="cordova.js"></script> 并排除 facebook web api 脚本,如果环境是生产环境,反之亦然

【问题讨论】:

  • 你采用了什么解决方案?
  • 它必须在它自己的脚本标签中吗?如果没有,您可以使用基于 env 的 require('path/to/file.js)

标签: javascript cordova webpack angular-cli


【解决方案1】:

找到了解决办法。

基于 Ionaru 在this issue 中的回答

在 main.ts 中:

if (environment.production) {

  document.write(
    `
      <script type="text/javascript">
         // JS code here
      </script>
    `
  );

  enableProdMode();
}

我相信这是有条件地嵌入代码的最简单方法,因为曾经直接在 index.html 上完成

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 2014-06-06
    • 2011-12-12
    相关资源
    最近更新 更多