The difference between $router and $route (notes)

1. $router is used to operate routing, and $route is used to obtain routing information.

2. $router is an instance object of VueRouter. It contains all routes, including routing jump methods, hook functions, and sub-objects (such as history).

3. $route is a jump routing object (routing information object). Each route will have a $route object, which is a local object.

$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 当前路由的名字,如果没有使用具体路径,则名字为空

Guess you like

Origin blog.csdn.net/limif/article/details/127359390