从插件访问Ember环境配置(ENV)?

时间:2017-02-02 12:46:36

标签: javascript ember.js

我有一个插件,它在开发和运行时为服务使用不同的主机。我相信这个解决方案是在应用程序的config / environment.js中定义主机名,并让服务选择如下:

import ENV from '../config/environment';

export default AjaxService.extend({
     host: ENV.APP.serviceHost,
     ...

然而,这不起作用,因为它认为environment.js文件的路径在插件的范围内而不是主应用程序。 当包含此插件的应用程序的名称未知时,执行此操作的正确方法是什么?

我正在使用Ember 2.11。

3 个答案:

答案 0 :(得分:4)

我知道有两种方法:

选项1

ember-config-service

此插件允许通过注入服务访问配置。它为我解决了同样的问题。

选项2

通过重新导出手动传递配置值。

// my-addon/app/services/my-ajax.js

// import config being in the app namespace
import ENV from '../config/environment';

import Service from 'my-addon/services/my-ajax';

export default Service.extend({
  apiURL: ENV.APP.apiURL
});

/// my-addon/addon/services/my-ajax.js

export default AjaxService.extend({
  init() {
    console.log('api url in addon', this.apiUrl);
  }
})

答案 1 :(得分:3)

使用最新版本的Ember,您可以从容器中获取环境配置。

Ember.getOwner(this).resolveRegistration('config:environment')

答案 2 :(得分:0)

我已经在我的项目中使用了这个包,效果很好:

https://github.com/null-null-null/ember-get-config

相关问题