admin管理员组文章数量:1384760
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 badges3 Answers
Reset to default 5I'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
本文标签: javascriptAccessing Ember environment config (ENV) from addonStack Overflow
版权声明:本文标题:javascript - Accessing Ember environment config (ENV) from addon? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744483432a2608305.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论