当从模板调用时,二级服务不会被注入
EmberJS Second level of service are not injected when calling from template
从模板(例如onClick={{action myFirstLevel.hello}}
)调用业务函数时,应该注入myFirstLevel
的服务留在undefined
。
通过组件操作调用正在工作。
<button onClick={{action myFirstLevel.hello}}>Hello Service</button>
<button onClick={{action 'hello'}}>Hello Action</button>
App = Ember.Application.create();
App.MyFirstLevelService = Ember.Service.extend({
mySecondLevel: Ember.inject.service(),
hello: function() {
console.log('Hello first level');
this.get('mySecondLevel').hello();
}
});
App.MySecondLevelService = Ember.Service.extend({
hello: function() {
console.log('Hello second level');
}
});
App.MyButtonComponent = Ember.Component.extend({
myFirstLevel: Ember.inject.service(),
actions: {
hello: function() {
this.get('myFirstLevel').hello();
}
}
});
http://emberjs.jsbin.com/conaxaheno/1/edit?html, js、控制台、输出
myFirstLevel.hello
函数将通过MyButtonComponent
调用此上下文,因此当您在服务中执行this.get('mySecondLevel')
时,它将返回undefined
,因此要使其工作,您应该将mySecondLevel
服务包含到组件
App.MyButtonComponent = Ember.Component.extend({
myFirstLevel: Ember.inject.service(),
mySecondLevel: Ember.inject.service(),
actions: {
hello: function() {
this.get('myFirstLevel').hello();
}
}
});
您可以使用hack。但是,除非你必须这样做,否则不建议这样做。我相信在你的情况下,最理想的方法是使用第二个按钮选项,但如果你绝对确定自己在做什么,你可以冒险一试:
App.MyFirstLevelService = Ember.Service.extend({
mySecondLevel: Ember.inject.service(),
hello: function() {
console.log('Hello first level');
Ember.getOwner(this).lookup('service:my-first-level').get('mySecondLevel').hello()
}
});
这个函数实际上会在你点击按钮时被调用,但它会在组件的上下文中被调用,这不是你想要的,因为当你瞄准this
时,你会访问组件作用域。
另一个选项是
App.MyFirstLevelService = Ember.Service.extend({
mySecondLevel: Ember.inject.service(),
hello: function() {
console.log('Hello first level');
this.get('myFirstLevel.mySecondLevel').hello()
}
});
在中,您可以避免与所有者混淆,但它仍然完全不可读。您能做的最好的事情就是坚持在组件作用域中定义操作,并从模板中调用它们。这些动作反过来可以为你获得任何深度的服务和触发功能。
@kumkanillam是对的,语境不对。要设置它,可以使用target=
,但onClick=...
似乎不起作用。
<button {{action myFirstLevel.hello target=myFirstLevel}}>Hello Service</button>
https://github.com/emberjs/ember.js/issues/14334 相关文章:
- 如何使用WCF服务和javascript表单post上传.doc文件
- 监视函数从服务返回不起作用,但作用域函数起作用
- 通过Magento的网络服务检索运费
- 在同一个服务工作者中处理service-worker.js有任何影响吗
- 使用angular.js问题的JavaSpringREST服务
- AngularJS-需要在index.html页面中访问来自服务的数据
- 如何在Ionic2测试版中包含Ionic.io服务
- 基于api密钥的NodeJS web服务
- Redux应用程序结构-在哪里放置服务/业务逻辑
- Liferay:从portlet调用JSON服务
- 使用javascript在客户端上使用Web服务
- 如何通过安全的https连接在javascript中使用基于soap xml的Web服务
- 从Javascript连接到SOAP Web服务
- 如何检测服务广告维度
- 很少有角服务,只有常数不同
- 如何在服务、技能、投资组合等方面添加滑动效果
- 尝试将服务链接到控制器时出现角度问题
- Sails.js:我可以将一些服务/模型打包为npm并在Sails.jss应用程序中加载npm吗
- 使用Object.create()的角度服务继承
- 当从模板调用时,二级服务不会被注入