1. 在需要保存方法的vue文件的 mounted 生命周期 里添加下列代码
//
mounted() {
let self = this;
this.$nextTick(() => {
let eventObj = {};
for (const selfItem in self) {
if (!selfItem.match(new RegExp("^[_\\$].*$"))) {
eventObj[selfItem] = self[selfItem];
}
}
this.$store.commit("setPageFunction", eventObj);
}
2. vuex中
mutations: {
setPageFunction(state, payload) {
state.pageFunction= payload
},
}
3. 在需要使用的vue组件中调用
this.pageFunction[fn]()
// 或者
this.pageFunction.xxx()