【问题标题】:how can i call the method from the promise in vue 2?我如何从 vue 2 中的 Promise 调用该方法?
【发布时间】:2018-07-08 05:12:20
【问题描述】:
 var cityPromise = new Promise(function (resolve, reject) {
      this.getAllCities();
      this.cities = this.cities.filter(data => {
        if (data.country_id === this.filCountry) {
          return data;
        }
      });
      resolve();

    });

我需要等到ajax调用的响应到达,然后对接收到的数据执行操作?

【问题讨论】:

  • 另外,即使我们看图中的代码也不清楚问题是什么。
  • 我会更新的。
  • 更新解决了一半的问题,请参阅我上面的(剩余的)评论。什么ajax调用?什么操作?什么数据? cities?你好像已经拥有了。更多:help centerHow do I ask a good question?
  • 如果这个 Promise 是在 Vue 方法中创建的,那么您需要使用箭头函数、闭包或绑定来定义您的回调,以便访问正确的 this。跨度>

标签: javascript promise vuejs2 vue-component


【解决方案1】:

一旦承诺返回,您将使用then

cityPromise.then(data => {
    // do something with data
})

【讨论】:

  • 我需要从 promise 中调用 getAllCities() 方法,但是这个变量在未定义的 promise 中不可用。
  • 我不关注,this.getAllCities() 不属于承诺。它是否在您的组件中定义?您是否希望从 Promise 中获得该调用的结果?
猜你喜欢
  • 2016-12-09
  • 2018-05-20
  • 2017-09-03
  • 2021-07-21
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
  • 2020-05-15
  • 2020-09-28
相关资源
最近更新 更多