ember cli环境配置

时间:2015-09-17 07:15:51

标签: ember.js ember-cli

请有人为此提供一个例子。我没有让它发挥作用。

http://www.ember-cli.com/user-guide/#Environments

我在config / environment.js文件中设置了一个环境变量。在路由操作中,我想访问此变量。但ENV未定义。

environment.js

if (environment === 'test') {
   ENV.APP.REPORTING_SERVICE_URL = 'http://www.purplerow.com';
}
if (environment === 'production') {
   ENV.APP.REPORTING_SERVICE_URL = 'http://www.stackoverflow.com';
}

路线

import ENV from 'my-demo-app/config/environment';

export default Ember.Route.extend({
   actions: {
     doSomething: function() {
       console.log(ENV.REPORTING_SERVICE_URL); // ENV is undefined
     }
   }
});

2 个答案:

答案 0 :(得分:1)

您正在以这种方式设置变量:

ENV.APP.REPORTING_SERVICE_URL = 'http://www.purplerow.com';

但是这样访问它:

ENV.REPORTING_SERVICE_URL

您应该在设置时删除APP,或者在获取时添加ENV.APP.REPORTING_SERVICE_URL ,例如:

undefined

如果是config/environment.js,请确保您没有引入任何可能破坏环境的语法。基本上,module.exports = function(environment) { var ENV = { modulePrefix: "my-demo-app", environment: environment, baseURL: "/", locationType: "auto" }; return ENV; }; 看起来应该是这样的:

ENV

请注意,您导出的变量的名称(在这种情况下,它是import)不必与您导入的变量名称匹配。它应该只导入在文件中导出的everthing并将其放入您在undefined中命名的变量中。如果是undefined,则基本上导出<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"> <Grid.ColumnDefinitions> <ColumnDefinition Width="400*"/> <ColumnDefinition Width="75*"/> </Grid.ColumnDefinitions> <WebView Source="{Binding ContentSource}" /> <!--<ProgressBar IsVisible="{Binding IsLoading}" Progress="{Binding Progress}"/>--> <Grid Grid.Column="1" BackgroundColor="#EE7F00" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"> <Label Text="{Binding DocumentIndex}" LineBreakMode="NoWrap" HorizontalOptions="Center" Rotation="-90" VerticalOptions="Center" /> </Grid> </Grid>

答案 1 :(得分:1)

请确保ENV中已归还./config/environment.js

一般情况下,它应如下所示:

module.exports = function(environment) {
  var ENV = {
    modulePrefix: 'app',
    environment: environment,
    baseURL: '/',
    locationType: 'auto',
    EmberENV: {
      FEATURES: {
        // Here you can enable experimental features on an ember canary build
        // e.g. 'with-controller': true
      }
    },

    APP: {
      // Here you can pass flags/options to your application instance when it is created
      // Also put default values here

      REPORTING_SERVICE_URL = 'http://www.stackoverflow.com'
    }
  };

  // overwrite default values for different environments as needed

  if (environment === 'development') {

  }

  if (environment === 'test') {
    ENV.APP.REPORTING_SERVICE_URL = 'http://www.purplerow.com';
  }

  if (environment === 'production') {

  }

  return ENV;
};

然后您可以在应用程序代码中使用它:

import ENV from './config/environment';

export default Ember.Route.extend({
   actions: {
     doSomething: function() {
       console.log(ENV.APP.REPORTING_SERVICE_URL);
     }
   }
});