1. 在需要保存方法的vue文件的 mounted 生命周期 里添加下列代码

  1. //
  2. mounted() {
  3. let self = this;
  4. this.$nextTick(() => {
  5. let eventObj = {};
  6. for (const selfItem in self) {
  7. if (!selfItem.match(new RegExp("^[_\\$].*$"))) {
  8. eventObj[selfItem] = self[selfItem];
  9. }
  10. }
  11. this.$store.commit("setPageFunction", eventObj);
  12. }

2. vuex中

  1. mutations: {
  2. setPageFunction(state, payload) {
  3. state.pageFunction= payload
  4. },
  5. }

3. 在需要使用的vue组件中调用

  1. this.pageFunction[fn]()
  2. // 或者
  3. this.pageFunction.xxx()