const state = {}
var copyState = deepClone(state) // 拷贝state对象
function deepClone (obj) {
var newObj = obj instanceof Array ? [] : {}
for (var i in obj) {
newObj[i] = typeof obj[i] === 'object' ? deepClone(obj[i]) : obj[i]
}
return newObj
}
const mutations = {
resetState (state) {
for (var i in copyState) {
state[i] = copyState[i] // 递归赋值
}
}
}
在登陆页添加
this.$store.commit('resetState')
本文介绍了一种使用深拷贝技术实现状态重置的方法。通过递归地复制对象属性,确保了状态的完全独立,避免了直接引用导致的问题。在登录页面中调用此功能,可以有效地初始化应用状态。
3969





