我可以从 Vuex 操作返回数据还是需要更新存储?
我定义了一个操作,但它不返回任何数据:
getData() {
return { "a" : 1, "b" : 2 }
}
最佳答案
您实际上可以从操作返回数据。来自 the documentation :
Actions are often asynchronous, so how do we know when an action is done? And more importantly, how can we compose multiple actions together to handle more complex async flows?
您应该在 resolve()
方法中返回一个 promise 和数据:
actions: {
actionA () {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ name: 'John Doe' })
}, 1000)
})
}
}
并以这种方式使用它:
store.dispatch('actionA').then(payload => {
console.log(payload) /* => { name: 'John Doe' } */
})
关于vue.js - 从 Vuex 操作返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54071406/