当前位置:  开发笔记 > 前端 > 正文

使用Angular CLI有条件地加载Angular2脚本?

如何解决《使用AngularCLI有条件地加载Angular2脚本?》经验,为你挑选了1个好方法。

我可以使用Angular CLI有条件地加载脚本吗?

在这个文件中,我想根据环境或变量加载一个脚本.所以在生产中我想在开发时加载脚本而不是.有没有办法做到这一点?怎么样?

角cli.json

...
"styles": [
    "../node_modules/bootstrap/dist/css/bootstrap.css",
    "../node_modules/font-awesome/css/font-awesome.min.css",
  ],
  "scripts": [
    "../node_modules/jquery/dist/jquery.js",
    "../node_modules/ion-rangeslider/js/ion.rangeSlider.js",
    "../node_modules/web-animations-js/web-animations.min.js",
    <---- LOAD HERE ONE SCRIPT DEPENDING ON ENVIRONMENT OR VARIABLE 
  ],
  "environments": {
    "source": "environments/environment.ts",
    "dev": "environments/environment.ts",
    "prod": "environments/environment.prod.ts"
  }
 ...

Leo.. 5

虽然@yuzuri建议了一个非常eleegnat解决方案 - 它需要你编辑一个不是一个好主意的节点模块..

这是一个简单的解决方法:

您可以编辑package.json文件下的"脚本"部分以获得:

"start": "cp angular-cli-dev.json angular-cli.json && ng serve"

"build": "cp angular-cli-prod.json angular-cli.json && ng build"

然后你应该将angular-cli.json文件重命名为angular-cli-dev.json和angular-cli-prod.json每个应该有不同的配置 - 在我的例子中,"脚本"部分中的不同脚本.

希望在等待官方解决方案时有所帮助



1> Leo..:

虽然@yuzuri建议了一个非常eleegnat解决方案 - 它需要你编辑一个不是一个好主意的节点模块..

这是一个简单的解决方法:

您可以编辑package.json文件下的"脚本"部分以获得:

"start": "cp angular-cli-dev.json angular-cli.json && ng serve"

"build": "cp angular-cli-prod.json angular-cli.json && ng build"

然后你应该将angular-cli.json文件重命名为angular-cli-dev.json和angular-cli-prod.json每个应该有不同的配置 - 在我的例子中,"脚本"部分中的不同脚本.

希望在等待官方解决方案时有所帮助

推荐阅读
linjiabin43
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有