Javascript 嵌套对象内列表的Vuex反应性问题
我试图使用vuex创建一个调度程序应用程序。为了实现调度,我使用了一个名为“scheduleController”的状态属性,它是一个对象,如下所示Javascript 嵌套对象内列表的Vuex反应性问题,javascript,vue.js,vuex,Javascript,Vue.js,Vuex,我试图使用vuex创建一个调度程序应用程序。为了实现调度,我使用了一个名为“scheduleController”的状态属性,它是一个对象,如下所示 const state = { scheduleController: { 2020: { 11: { month: "November", count: [{ days: [], ti
const state = {
scheduleController: {
2020: {
11: {
month: "November",
count: [{
days: [],
timers: [{
from: null,
interval: null,
to: null
}]
}]
}
12: {
month: "December"
...
}
}
}
}
每当我按下“添加”按钮时,计时器数组都应添加与第一个对象相同的另一个对象,以便在所选日期内为月份创建多个计划。为此,我使用了以下变异:
addTimer: (state, indices) => {
let timer = {
from: null,
to: null,
interval: null,
};
//indices[0] gives the year and indices[1] gives month-index
//and indices[2] gives the nth element of count array in which
//an empty timer has to be added.
Vue.set(state.scheduleController[indices[0]][indices[1]].count[indices[2]].timers, 1, timer)
}
状态会按照我的意愿发生变化,但它不是被动的。我必须转到另一个视图,然后回来看看状态的变化。我怎样才能让它反应
在前面的代码中,我使用了
state.scheduleController[indices[0]][indices[1]].count[indices[2]].timers.push(timer)
代替Vue.set函数。但是在看了这个链接之后,我把它改成了现在的样子,但还是不起作用。谢谢
解决了这个问题
addTimer: (state, indices) => {
let timer = {
from: null,
to: null,
interval: null,
};
let newScheduleController = JSON.parse(JSON.stringify(state.scheduleController))
newScheduleController[indices[0]][indices[1]].count[indices[2]].timers.push(timer)
state.scheduleController = newScheduleController
},