Angular2 使用 Angular CLI 有条件地加载脚本? [英] Angular2 load script conditionally using Angular CLI?

查看:24
本文介绍了Angular2 使用 Angular CLI 有条件地加载脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

angular-cli.json

<预><代码>...风格":["../node_modules/bootstrap/dist/css/bootstrap.css","../node_modules/font-awesome/css/font-awesome.min.css",],脚本":["../node_modules/jquery/dist/jquery.js","../node_modules/ion-rangeslider/js/ion.rangeSlider.js","../node_modules/web-animations-js/web-animations.min.js",<---- 根据环境或变量在此处加载一个脚本],环境":{"source": "环境/environment.ts","dev": "环境/environment.ts","prod": "环境/environment.prod.ts"}...

解决方案

虽然@yuzuri 提出了一个非常优雅的解决方案 - 它需要您编辑一个节点模块,这不是一个好主意..

这里有一个简单的解决方法:

您可以编辑 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每个都应该有不同的配置 - 在我的情况下,脚本"部分中的不同脚本.

希望在我们等待官方解决方案期间有所帮助

Can I load scripts conditionally using Angular CLI???

In this file, I want to load one script depending on the enviroment or a variable. So In production I want to load a script while in develop not. Is there a way to do that? How?

angular-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"
  }
 ...

解决方案

While @yuzuri suggested a very eleegnat solution - it requires you to edit a node module which is not a good idea..

Here is simple workaround:

You can edit your "scripts" section under package.json file to have that:

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

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

Then you should rename your angular-cli.json file to angular-cli-dev.json and angular-cli-prod.json Each should have different configuration - in my case, different scripts in "scripts" section.

Hope that helps while we wait for an official solution

这篇关于Angular2 使用 Angular CLI 有条件地加载脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆