grunt-ng-constant不会生成configuration脚本

我想在我的AngularJS中使用环境variables来做环境特定的configuration。 我正在使用使用Grunt的Yeoman工作stream程,而grunt-ng-constant插件旨在帮助进行环境特定的configuration。 在本教程中 ,我设置了相应的Grunt文件,但是当我在控制台中运行grunt serve时, config.js没有写入/app/scripts/ 。 没有config.js ,我不能将环境variables注入到angular度应用程序中。

这是我的Gruntfile的一个片段:

 module.exports = function (grunt) { // Time how long tasks take. Can help when optimizing build times require('time-grunt')(grunt); // Automatically load required Grunt tasks require('jit-grunt')(grunt, { useminPrepare: 'grunt-usemin', ngtemplates: 'grunt-angular-templates', cdnify: 'grunt-google-cdn' }); // Configurable paths for the application var appConfig = { app: require('./bower.json').appPath || 'app', dist: '../server/dist' }; grunt.loadNpmTasks('grunt-contrib-clean'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-ng-constant'); // Define the configuration for all the tasks grunt.initConfig({ // Project settings yeoman: appConfig, ngconstant: { // options for all environments options: { space: ' ', wrap: '"use strict";\n\n {%= __ngModule %}', name: 'config' }, // Development/Testing environment development: { options: { dest: '<%= yeoman.app %>/scripts/config.js' }, constants: { ENV: { name: 'development', apiEndpoint: 'http://localhost:3000' } } }, // Production environment production: { options: { dest: '<%= yeoman.dist %>/scripts/config.js' }, constants: { ENV: { name: 'production', apiEndpoint: 'http://productionUrl' } } } }, ... grunt.registerTask('serve', 'Compile then start a connect web server', function (target) { if (target === 'dist') { return grunt.task.run(['build', 'connect:dist:keepalive']); } grunt.task.run([ 'clean:server', 'wiredep', 'concurrent:server', 'autoprefixer:server', 'connect:livereload', 'watch', 'ngconstant:development' ]); }); ... 

生成的是与/.tmp相同的目录( client )中的/.sass-cache和/.tmp。

我的应用程序文件结构

在这里输入图像描述

ngconstant任务没有被调用,因为它在watch任务之后。 修改运行块,使'ngconstant:development' 'autoprefixer:server',旁边'autoprefixer:server',所以在连接和监视任务之前。 不要忘了添加一个逗号!

  grunt.task.run([ 'clean:server', 'wiredep', 'concurrent:server', 'autoprefixer:server', 'ngconstant:development', 'connect:livereload', 'watch' ]); 

此外,应用程序path可能在bower.json文件中是错误的。 可以肯定的是,通过更改appConfig来修改您的应用的path,使其看起来像这样:

 var appConfig = { app: 'app', dist: '../server/dist' }