我正在尝试从另一个函数的Axios调用中访问Promise值。

methods: {
    checkItem() {
        const url = "http://api.crossref.org/journals/" + this.editedItem.issn;
        return axios.get(url)
            .then(response => response.status);
    },

    save() {
        console.log(this.checkItem());
    }
}


我希望在调用save()时将状态代码记录在控制台中。目前,它正在记录整个承诺。

最佳答案

您可以使用async/await

<script>
new Vue({
  el: "#root",

  methods: {
    checkItem() {
      const url = "http://api.crossref.org/journals/" + this.editedItem.issn;
      return axios.get(url).then(response => response.status);
    }
  },
  async save() {
    console.log(await this.checkItem());
  }
});
</script>


或使用Promise

<script>
new Vue({
  el: "#root",

  methods: {
    checkItem() {
      const url = "http://api.crossref.org/journals/" + this.editedItem.issn;
      return axios.get(url).then(response => response.status);
    }
  },
  save() {
    this.checkItem().then(res => {
      console.log(res);
    });
  }
});
</script>

关于javascript - 访问 promise 值axios,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57188998/

10-13 01:53