我正在研究Ember应用程序..
它使用Ember.inject.service模块 - 并且它没有解决承诺 - 不提供我需要的字符串
我有一个看起来像这样的服务
let downloadMethod = this.get('service1')
.fetch(id)
.then(res => {
console.log("res", res.agenda.method.value)
res.agenda.method.value
});
console.log("downloadMethod", downloadMethod);
当我尝试访问它时 - downloadmethod的控制台日志显示了一个承诺 - 但在then-res中出现了我需要的字符串值。
如何从“downloadMethod”中获取值 - 它显示为promise,而不是字符串结果?
我是否需要将它包裹在Ember.RSVP.hash周围?
答案 0 :(得分:2)
在承诺完成之前调用console.log("downloadMethod", downloadMethod)
,因此您还没有字符串值。你只有未解决的承诺。
因此您需要将值保存在then
函数中。下面是一些代码,用于显示如果它位于Ember组件
import Component from '@ember/component';
import { inject as service } from '@ember/service';
export default Component.extend({
service1: service(),
fetchedValue: null,
actions: {
someAction() {
this.get('service1')
.fetch(id)
.then(res => {
this.set('fetchedValue', res.agenda.method.value);
});
}
}
})
此代码为Ember版本2.16及以上
答案 1 :(得分:-1)
对此的实际解决方案是在模型父级中创建返回RSVP.hash。
model () {
return RSVP.hash({
deliveryMethod: this.get('service1').fetch(this.get('service2').id).then(res => {
return res.agenda.method.value;
})
});
},