[英]How can I pass argument with parameter to gulp?
I want to do something like 我想做类似的事情
gulp build --param1=abc
and I want this param to be available in Angular Service/Controller. 我希望在Angular Service / Controller中可以使用此参数。 Is there a way I can do that?
有办法吗?
Yes. 是。 I would recommend in your Gulp config, you create an Angular constant that can be injected into any part of the application.
我建议您在Gulp配置中创建一个Angular常数,该常数可以注入到应用程序的任何部分。
var ngConstant = require('gulp-ng-constant');
var args = require('yargs').argv;
gulp.task('constant', function () {
var selectedConstant = args.env || 'dev'; // if --env is not declared, assume dev
var constants = {
dev: {
ENV: {
'api': 'api.someUrl.com',
'url': 'dev.url.com'
}
},
prod: {
ENV: {
'api': 'api.someUrl.com',
'url': 'dev.url.com'
}
},
};
return ngConstant({
name: 'app', // name of your module
constants: constants[selectedConstant],
stream: true
})
.pipe(gulp.dest('src'));
});
gulp.task('build', function () {
runSequence(
['constant']
)
});
Now, you can run gulp build --env=prod
and that will create a file called constants.js
in your /src/
folder (or wherever you specify). 现在,您可以运行
gulp build --env=prod
,这将在/src/
文件夹(或指定的任何位置)中创建一个名为constants.js
文件。 Now include this file in your index.html
. 现在,将此文件包含在
index.html
。
You can now inject the constant ENV
anywhere in your services or controllers and reference the variables within. 现在,您可以在服务或控制器中的任何位置注入常量
ENV
,并引用其中的变量。
angular.module('app')
.service('SomeService', function(ENV) {
// access ENV vars here
});
.controller('SomeCtrl', function($scope, ENV) {
// access ENV vars here
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.