Ember promise - 取值 - 使用RSVP方式?

Ember promise - accessing value - use the RSVP method?

我正在开发 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" 中获取值——它显示为承诺,而不是字符串结果?

我需要用 Ember.RSVP.hash 包裹它吗?

console.log("downloadMethod", downloadMethod) 在承诺完成之前调用,因此您还没有字符串值。你只是有未完成的承诺。

因此您需要将值保存在then 函数中。下面是一些代码,用于显示如果它在 Ember Component

中的样子
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);
        });
    }
  }
})

此代码为Ember2.16及以上版本

实际的解决方案是在模型父级中创建一个 return RSVP.hash。

model () {
    return RSVP.hash({
      deliveryMethod: this.get('service1').fetch(this.get('service2').id).then(res => {
         return res.agenda.method.value;
      })
    });
},