我有以下初始化程序无法正常工作:
Ember.onLoad 'Ember.Application', (Application) ->
Ember.Application.initializer
name: 'storeToComponent'
initialize: (container, application) ->
application.inject('component', 'store', 'store:main')
Run Code Online (Sandbox Code Playgroud)
该组件没有商店属性.我尝试了很多东西,比如:
before: 'registerComponents'
Run Code Online (Sandbox Code Playgroud)
但没有任何作用.
我究竟做错了什么?
您的组件将始终位于某些上下文中(例如,控制器),因此您可以通过在组件中执行来从该上下文访问存储:
App.MyComponent = Ember.Component.extend({
actions: {
foo: function() {
var store = this.get('targetObject.store');
}
}
});
Run Code Online (Sandbox Code Playgroud)
但是,应该说由于组件被认为是隔离的,因此您应该将数据传递给它们,而不是从特定存储创建依赖关系.也就是说,如果您真的希望将商店注入您的组件,您可以尝试这样做:
Ember.onLoad 'Ember.Application', (Application) ->
Ember.Application.initializer
name: 'storeToComponent'
before: 'registerComponents'
initialize: (container, application) ->
application.register('store:main', App.Store)
application.inject('component', 'store', 'store:main')
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.
| 归档时间: |
|
| 查看次数: |
1871 次 |
| 最近记录: |