$router和$route区别(笔记)

1、$router用来操作路由,$route用来获取路由信息

2、$router是VueRouter的一个实例对象,他包含了所有的路由,包括路由的跳转方法,钩子函数,子对象(例如history)

3、$route是一个跳转的路由对象(路由信息对象),每一个路由都会有一个$route对象,是一个局部的对象

$router:

//常规方法
this.$router.push("/login");
//使用对象的形式 不带参数
this.$router.push({ path:"/login" });
//使用对象的形式,参数为地址栏上的参数
this.$router.push({ path:"/login",query:{username:"jack"} }); 
使用对象的形式 ,参数为params 不会显示在地址栏
this.$router.push({ name:'user' , params: {id:123} });

$route

主要的属性有:
this.$route.path 字符串,等于当前路由对象的路径,会被解析为绝对路径,如/home/ew
 
this.$route.params 对象,包含路由中的动态片段和全匹配片段的键值对,不会拼接到路由的url后面
 
this.$route.query 对象,包含路由中查询参数的键值对。会拼接到路由url后面
 
this.$route.router 路由规则所属的路由器
 
this.$route.name 当前路由的名字,如果没有使用具体路径,则名字为空

猜你喜欢

转载自blog.csdn.net/limif/article/details/127359390