【问题标题】:Getting Axios params to work in Async/Await function让 Axios 参数在 Async/Await 函数中工作
【发布时间】:2020-08-21 15:11:49
【问题描述】:

我在这次通话中遗漏了什么? asyncData 的异步特性会影响请求加载参数的能力吗?网络选项卡返回一个304 代码,但没有从后端获取任何数据。

import { mapGetters } from "vuex";

export default {
  computed: {
    ...mapGetters(["loggedInUser"])
  },
  head() {
    return {
      title: "Actors list"
    };
  },
  components: {
    EditProfile
  },
  async asyncData() {
    try {
      let actors = await $axios.$get(`/api/v1/actors/`, {
        params: {
          user: this.loggedInUser.id
        }
      });
      
      return { actors };
    } catch (e) {
      return { actors: [] };
    }
  },

【问题讨论】:

  • 304 表示数据被缓存(不调用后端)。清除缓存并重试。
  • @HassenCh。嘿,刚刚试过,没有数据就得到了同样的东西。还有其他可能的修复方法吗?
  • 我猜你应该分享更多代码。你能添加你的后端代码吗?

标签: vue.js vuejs2 axios vuex nuxt.js


【解决方案1】:

更新:

只要意识到 asyncdata 不允许使用 'this' 或存储中的其他变量,因为它是首先要加载的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-07
    • 2017-10-24
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 2019-10-04
    相关资源
    最近更新 更多