【问题标题】:how to call multiple function in fetch hook in nuxt, vue如何在nuxt,vue的fetch钩子中调用多个函数
【发布时间】:2021-02-04 13:49:46
【问题描述】:

当我使用 await 从 fetch 调用 2 个函数时,我遇到了在 fetch 挂钩 nuxt 中调用多个函数的问题。

async fetch() {
        await this.fetchData();
        await this.fetchData2();
    },
    methods: {
        fetchData(lang) {
            this.$store.dispatch('getData')
        },
        async fetchData2() {
            let res = await api.getData2();
        },
    }

这是我在项目运行时使用的流程,出现错误

TypeError: Cannot read property 'toLowerCase' of undefined and TypeError: progress.start is not a function

我正在使用 nuxt 进度加载。请让我知道我做错了什么

【问题讨论】:

    标签: vuejs2 nuxt.js vuex


    【解决方案1】:

    您正在等待this.fetchData(),但这并没有返回承诺。

    您想添加await this.$store.dispatch('getData')。您也没有使用lang 参数。

    换句话说,我想以下方法会起作用......

    async fetch() {
            await this.fetchData();
            await this.fetchData2();
        },
        methods: {
            async fetchData() {
                await this.$store.dispatch('getData')
            },
            async fetchData2() {
                let res = await api.getData2();
            },
        }
    

    (或者甚至只是返回 this.$store.dispatch('getData') 而没有 fetchData 是异步 fn)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-06
      • 2020-10-24
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 2020-07-16
      • 2016-12-09
      • 1970-01-01
      相关资源
      最近更新 更多