1.访问

utils下新建 vueApi.js
import { getCurrentInstance } from "vue";

// 访问vuex
export const useStore = () => {
  const vm = getCurrentInstance();
  if (!vm) throw new Error("must be called in setup");
  return vm.proxy.$store;
};
// 访问router
export const useRouter = () => {
  const vm = getCurrentInstance();
  if (!vm) throw new Error("must be called in setup");
  return vm.proxy.$router;
};
// 访问route
export const useRoute = () => {
  const vm = getCurrentInstance();
  if (!vm) throw new Error("must be called in setup");
  return vm.proxy.$route;
};
使用
import { computed, onMounted } from "vue";
import { useStore, useRouter, useRoute } from "vueApi.js";

const message = computed(() => {
  return useStore().state.message;
});
onMounted(() => {
  // console.log(useRoute());
  // console.log(message.value);
  // useStore().commit("showErrMsgFun", "123123");
  // useRouter().push("/MigrationNotifications");
});

2.修改vuex

import store from "@/views/home/list/store.js";

const setUserName = (name) => {
  // 在vue2.7里如何使用composition Api修改vuex... 
  store.state.user.name = name;
};

Logo

前往低代码交流专区

更多推荐