【问题标题】:How to write an async function in a vuex module如何在 vuex 模块中编写异步函数
【发布时间】:2021-05-19 20:32:51
【问题描述】:

我正在使用 Nuxt fetch() lifecycle hook 从 Vuex 获取数据。我的商店结构如下所示

 store/
--| index.js
--| dialog.js
--| country/
------| actions.js    // I want to use async call in this file
------| getters.js
------| mutations.js
------| state.js 

在我的组件中,我正在使用这样的 fetch

async fetch({ store }){
   await store.dispatch("country/fetchCountries")
},

我的国家/actions.js 文件如下所示

export const fetchCountries = ({commit, dispatch}, form) => {

    // let response = await this.$axios.get('/countries'); 

}

现在,我不能在函数前面使用 async。这个函数前面怎么用async?

【问题讨论】:

    标签: javascript vue.js nuxt.js vuex


    【解决方案1】:

    这个不工作?

    export const fetchCountries = async ({commit, dispatch}, form) => {

    【讨论】:

    • 感谢您的解决方案。它有效,但出现了另一个问题。 this.$axios 不工作。但我发现了另一种解决方案。我把所有函数都包裹在export default{ async fetchCountries(){ } }
    猜你喜欢
    • 2021-12-19
    • 2021-12-31
    • 2011-10-17
    • 2020-05-17
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多