Vuex - 错误“无法读取未定义的属性(读取“设置”)”当使用 Vue.set() 时
尝试使用 Vue.set() 时,我在控制台中收到以下错误:
我缺少导入吗?我正在尝试更新对象数组 - 有人可以帮忙吗?
谢谢
import { VueElement } from 'vue'
import Vue from 'vue'
import Vuex from 'vuex'
import { createStore } from 'vuex'
export default createStore({
state: {
activeLightScenes: [
{'room': null, 'scene': null},
{'room': null, 'scene': null},
{'room': null, 'scene': null},
{'room': null, 'scene': null},
]
},
mutations: {
updateLightScenes(state, room, scene){
Vue.set(state.activeLightScenes, 'room', room, 'scene', scene)
},
},
getters:{
},
actions: {
},
modules: {
},
})
I am getting the following error in console when trying to use Vue.set():
Am I missing imports? I am trying to update an array of objects - please can someone help?
Thanks
import { VueElement } from 'vue'
import Vue from 'vue'
import Vuex from 'vuex'
import { createStore } from 'vuex'
export default createStore({
state: {
activeLightScenes: [
{'room': null, 'scene': null},
{'room': null, 'scene': null},
{'room': null, 'scene': null},
{'room': null, 'scene': null},
]
},
mutations: {
updateLightScenes(state, room, scene){
Vue.set(state.activeLightScenes, 'room', room, 'scene', scene)
},
},
getters:{
},
actions: {
},
modules: {
},
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能安装了 Vue 3,并且
vue@3
没有默认导出,因此Vue
将为undefined
,从而导致您观察到的错误Vue.set
。但是,在 Vue 3 中,您不需要使用像Vue.set
这样的东西。等效的代码只是直接设置变量。假设您还传递了
state.activeLightScenes[]
中元素的数组索引,该索引将包含指定的room
和scene
参数:You likely have Vue 3 installed, and
vue@3
has no default export, soVue
would beundefined
, leading to the error you observed withVue.set
. However, in Vue 3, you don't need to use something likeVue.set
. The equivalent code would just be setting the variable directly.Assuming you also pass an array index of the element in
state.activeLightScenes[]
that is to contain the specifiedroom
andscene
arguments: