我可以使用 Angular CLI 有条件地加载脚本吗???
在此文件中,我想根据环境或变量加载一个脚本。所以在生产中我想在开发时加载脚本而不是。 有没有办法做到这一点?怎么办?
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 提出了一个非常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 每个都应该有不同的配置——在我的例子中,“脚本”部分中的不同脚本。
希望在我们等待官方解决方案时有所帮助
关于angularjs - Angular2 使用 Angular CLI 有条件地加载脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41741457/