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

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

问题描述

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

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

推荐答案

@yuzuri提出了一个非常有效的解决方案-它要求您编辑节点模块,但这不是一个好主意.

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:

您可以在package.json文件下编辑脚本"部分以使其具有以下功能:

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"

然后,您应该将angular-cli.json文件重命名为angular-cli-dev.json和angular-cli-prod.json 每个脚本都应具有不同的配置-就我而言,脚本"部分中的脚本是不同的.

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天全站免登陆