vue3中使用route、router、store的方式

这篇文章介绍了Vue3和Vue2中处理路由、路由器和Vuex状态管理的不同写法。在Vue3中,使用`useRoute`获取路由信息,`useRouter`进行导航操作,而`useStore`用于Vuex的state访问、提交mutation和派发action。相比之下,Vue2中这些功能分别通过`$route`,`$router`和`$store`对象实现。

route:

 (1) vue3写法:

import { useRoute } from "vue-router";

let route = useRoute();

route.query;
route.params;

 (2) vue2写法:

import { getCurrentInstance } from "vue";

let { proxy: this_ }: any = getCurrentInstance();

this_.$route.query;
this_.$route.params;

 router:

 (1) vue3写法:

import { useRouter } from "vue-router";

let router = useRouter();

router.push("/home");
router.replace("/home");
router.go(1);

 (2) vue2写法:

import { getCurrentInstance } from "vue";

let { proxy: this_ }: any = getCurrentInstance();

this_.$router.push("/home");
this_.$router.replace("/home");
this_.$router.go(1);

 store:

 (1) vue3写法:

import { useStore} from "vuex";

let store= useStore();

store.state.list;
store.commit("getList",res.data);
store.dispatch("getList",res.data);
store.getters.getList;

 (2) vue2写法:

import { getCurrentInstance } from "vue";

let { proxy: this_ }: any = getCurrentInstance();

this_.$store.state.list;
this_.$store.commit("getList",res.data);
this_.$store.dispatch("getList",res.data);
this_.$store.getters.getList;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值