【发布时间】:2021-05-03 13:11:50
【问题描述】:
我在我的 vuex 商店中有一个动作:
export const actions = {
myaction() {
return 'foo'
}
}
我可以在mounted() 生命周期钩子中得到承诺结果(这里是foo)并在控制台中显示它吗?如果是,如何?
我试过了:
mounted() {
console.log(
this.$store
.dispatch('myaction')
.then(res => res)
)
}
但它返回的是承诺而不是我期望的承诺结果。
【问题讨论】:
-
我可能误解了您要查找的内容,但
.then(res => console.log(res))不会解决问题吗? -
@nikoshr 我什至可以通过
this.$store.dispatch('myaction').then(console.log)在控制台中获取foo,但实际上,我需要在控制台或let myvar = this.$store.dispatch('myaction')之类的变量中返回foo,但是当我想要myvar中的结果时,这会返回承诺@ -
可以,但不应该这样做,因为它违反了单向数据流范式。您的目标应该是执行一个操作、设置一些数据并使用 getter 来检索它。