Vue.js 发送接收未定义为值
我在发送后更新状态时遇到问题 控制台记录Vue.js 发送接收未定义为值,vue.js,vuex,nuxt.js,Vue.js,Vuex,Nuxt.js,我在发送后更新状态时遇到问题 控制台记录dispatch的结果,显示Promise pending和valueundefined,因此永远不会到达商店 下面是调用dispatch处理程序的函数 unsetSelected() { let some = this.$store.dispatch('user/selectedDevice', null) console.log(some) } <span class="ellipsis" @click="setSelected
dispatch
的结果,显示Promise pending
和valueundefined
,因此永远不会到达商店
下面是调用dispatch
处理程序的函数
unsetSelected() {
let some = this.$store.dispatch('user/selectedDevice', null)
console.log(some)
}
<span class="ellipsis" @click="setSelected(device)">
<i v-if="selectedDevice && selectedDevice.id == device.id"
@click="unsetSelected()"
class="fa fa-times-circle">
</i>
<i v-else="" class="fa fa-ellipsis-v"></i>
</span>
从实例方法的Vuex API参考: 返回解析所有触发操作处理程序的承诺 因此调用
dispatch
返回一个。为了在组件内部接收数据,应该解决这个问题
您可以将组件的方法修改为以下内容:
// using async/await
async unsetSelected() {
try {
let some = await this.$store.dispatch('user/selectedDevice', null)
console.log(some)
} catch (error) {
// handle error
}
}
// using Promise API
unsetSelected() {
this.$store.dispatch('user/selectedDevice', null)
.then((some) => {
console.log(some)
})
.catch((error) => {
// handle error
})
}
而且,selectedDevice
不会返回任何数据,因此来自已解析承诺的部分(或响应)将是未定义的
,例如所提供的有问题的代码
要解决这个问题,store操作应该有一个return
语句,其中包含要返回到组件的所需数据
不过,根据Vuex体系结构,我们建议这样做,在提交状态变异后,将对其值进行反应性更新。我对变异处理程序发出警报,发现代码按预期工作,只是它还在DOM中启动了上面的dispatch
函数
我必须用.stop
修饰符链接它:@click.stop=“unsetSelected()”
您建议我做的所有操作,但仍然返回未定义。我已为所选设备设置了getter,但该设备仍不工作@AbisCuit您是否能够解决承诺,但它仍然显示未定义
?您希望在哪里接收值?我希望在存储中接收export const state=()=>({devices:null,selectedDevice:null})
然后通过computed
属性computed:{selectedDevice(){返回此值。$store.getters['user/selectedDevice']}
请在问题中添加选定的\u设备
变异代码,以验证所有内容是否都在更新。
// using async/await
async unsetSelected() {
try {
let some = await this.$store.dispatch('user/selectedDevice', null)
console.log(some)
} catch (error) {
// handle error
}
}
// using Promise API
unsetSelected() {
this.$store.dispatch('user/selectedDevice', null)
.then((some) => {
console.log(some)
})
.catch((error) => {
// handle error
})
}