前端vue2.6升级至vue2.7后如何使用旧版本vue-router与vuex,并且修改vuex
vue2.6项目升级至vue2.7遇到的问题解决办法
·
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;
};
更多推荐
已为社区贡献3条内容
所有评论(0)