【问题标题】:How to specify a variable for an AngularJS app as a command line parameter when starting a Grunt dev server (grunt-contrib-connect)如何在启动 Grunt 开发服务器时将 AngularJS 应用程序的变量指定为命令行参数(grunt-contrib-connect)
【发布时间】:2015-01-12 18:37:03
【问题描述】:

在我的生产环境中,Angular 应用程序在与 API 相同的域下提供服务,因此基本 url 的形式为例如'/api'。

问题是当我开发时,我使用 grunt serve (grunt-contrib-connect) 作为我的开发服务器,我想在启动它时为 API Endpoint 指定一个基本 url,例如grunt serve --endpoint="http://localhost/api"

我知道我可以使用 grunt.option 获得这个值,但是我不知道如何将它传递给我的 Angular 应用程序。

注意:这不是典型的生产/开发配置拆分。我希望每次启动开发服务器时都指定端点。

有什么解决办法吗?越简单越好!

【问题讨论】:

    标签: angularjs gruntjs yeoman yeoman-generator-angular grunt-contrib-connect


    【解决方案1】:

    这就是你想要的:https://github.com/jsoverson/grunt-env

    在您的 Gruntfile.js 中,您将指定 ENV 变量:

    grunt.initConfig({
      env: {
        development: {
          ENV: 'development'
        },
    
        production: {
          ENV: 'production'
        }
      },
    
      // ...
    

    在你的客户端代码的某个地方,比如在我的 settings.js 文件中,你会做这样的事情:

    // @if ENV='development'
    'api_base_url': 'http://localhost:' + (apiPort || 4000),
    'base_url': 'http://localhost:7777',
    'env': 'development'
    // @endif
    

    【讨论】:

      猜你喜欢
      • 2014-05-29
      • 1970-01-01
      • 2015-01-25
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 2014-11-19
      相关资源
      最近更新 更多