[英]How can I get the namespace of the including application in an Ember CLI Addon?
我正在創建一個Ember CLI ember-addon
,並且在我的附件文件中,我需要訪問該應用程序的配置。 我無法知道包含此插件的應用程序將被命名為什么,所以我不能像在應用程序本身中那樣簡單地import ENV from 'app-name/config/environment'
。
如何從插件本身內部訪問使用ember-addon
的應用程序的名稱空間,以便可以從該應用程序導入內容?
您無需獲取名稱空間即可獲取配置。
您的插件所需的任何設置都應添加到config/environment.js
ENV.APP
上。
例如,如果您想要MY_APP_KEY
設置,則需要類似
ENV.APP.MY_APP_KEY = 'ABCDEF';
已添加到config/environment.js
。
然后,您可以使用初始化程序從應用程序實例中讀取屬性,然后通過執行以下操作將其注入到您的插件中:
export default {
name: "my initilizer",
initialize: function(container, app) {
//get you setting off of the app instance
var key = app.get('MY_APP_KEY');
//register it
app.register('config:myAddonKey', key, { instantiate: false });
//inject it where you want to access it
app.inject('route', 'myAddonKey', 'config:myAddonKey');
}
};
您可以在Ember Django Adapter中看到有關其完成方式的示例
一種可能是使用initializer
:
Ember.Application.initializer({
name: 'my-component',
initialize: function(container, app) {
// you have access to 'app' here...
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.