1、state 定义变量(可以动态创建)
state: {
tradecfgTree: getStore({ name: "tradecfgTree" }) || []
},
2、mutations 给state赋值
setTradeCfgTree(state, data) {
state.tradecfgTree = data.res;
}
3、actions 异步方法
调用api获取数据然后通过mutations赋值
async getTradeCode(context) {
let dicData = getStore({ name: "tradecfgTree" });
if (dicData) {
console.log(1);
context.commit("setTradeCfgTree", {
dicCodes: "tradecfgTree",
res: dicData
});
} else {
const res = await getTradeCfgTree();
let dic = res.data.data;
setStore({
name: "tradecfgTree",
content: dic,
type: "session"
});
context.commit("setTradeCfgTree", {
dicCodes: "tradecfgTree",
res: dic
});
}
}
4、getters 相当于computed
从state获取值