最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Accessing Ember environment config (ENV) from addon? - Stack Overflow

programmeradmin4浏览0评论

I have an addon which uses a different host for a service in development and at runtime. I believed the solution to this would be to define the hostname in the config/environment.js of the application and to have a the service pick it up as follows:

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

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

This however doesn't work as it believes that the path to the environment.js file is within the scope of the addon instead of the main application. What is the right way to do this when the name of the application in which this addon is included is not known?

I am using Ember 2.11.

I have an addon which uses a different host for a service in development and at runtime. I believed the solution to this would be to define the hostname in the config/environment.js of the application and to have a the service pick it up as follows:

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

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

This however doesn't work as it believes that the path to the environment.js file is within the scope of the addon instead of the main application. What is the right way to do this when the name of the application in which this addon is included is not known?

I am using Ember 2.11.

Share Improve this question edited Feb 2, 2017 at 13:15 vogomatix asked Feb 2, 2017 at 12:46 vogomatixvogomatix 5,0912 gold badges26 silver badges49 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

I'm aware of 2 approaches for this:

Option 1

ember-config-service

This addon allows to access config via injected service. It solved the same issue for me.

Option 2

Manually pass config value via re-export.

// 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);
  }
})

With recent versions of Ember, you can get the environment config from the container.

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

I've used this package in my projects to good effect:

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

发布评论

评论列表(0)

  1. 暂无评论