更多数据在状态中传递,未正确获取
more data passed in state, it is not taken correctly
我有现成的常量需要保存在状态中:
const day = "25/02/2020";
const timeStart = "08:00";
const timeEnd = "00:00";
在我的 vuex 文件中我有这个:
export default new Vuex.Store ({
state: {
dateSelected: [], // selected date
},
mutations: {
saveDateSelected (state, [newDateSelected, newTimeStart, newTimeEnd]) {
const newobject = {
DateStart: newDateSelected + "-" + newTimeStart,
DateEnd: newDateSelected + "-" + newTimeEnd,
};
state.dateSelected.push (newobject);
}
},
在我的组件中,我使用以下方法调用数据:
this.saveDateSelected (day, timeStart, timeEnd);
如果我执行“console.log (this.dateSelected);”结果给我:
DateEnd: "2 - /"
DateStart: "2 - 5"
什么时候应该给我:
DateEnd: "25/02/2020 - 00:00"
DateStart: "25/02/2020 - 08:00"
您不应该直接从组件调用商店变更方法。改为提交
this.$store.commit('saveDateSelected, [day, timeStart, timeEnd])
还要确保通过组件的 computed
属性 正确访问组件中的 vuex 存储。
我有现成的常量需要保存在状态中:
const day = "25/02/2020";
const timeStart = "08:00";
const timeEnd = "00:00";
在我的 vuex 文件中我有这个:
export default new Vuex.Store ({
state: {
dateSelected: [], // selected date
},
mutations: {
saveDateSelected (state, [newDateSelected, newTimeStart, newTimeEnd]) {
const newobject = {
DateStart: newDateSelected + "-" + newTimeStart,
DateEnd: newDateSelected + "-" + newTimeEnd,
};
state.dateSelected.push (newobject);
}
},
在我的组件中,我使用以下方法调用数据:
this.saveDateSelected (day, timeStart, timeEnd);
如果我执行“console.log (this.dateSelected);”结果给我:
DateEnd: "2 - /"
DateStart: "2 - 5"
什么时候应该给我:
DateEnd: "25/02/2020 - 00:00"
DateStart: "25/02/2020 - 08:00"
您不应该直接从组件调用商店变更方法。改为提交
this.$store.commit('saveDateSelected, [day, timeStart, timeEnd])
还要确保通过组件的 computed
属性 正确访问组件中的 vuex 存储。