router 和 route

2021-04-09  本文已影响0人  酒暖花深Q

1. this.$route 当前的路由对象(是当前的路由信息)

在使用了vue-router 的应用中,路由对象会被注入到每个组件中,赋值为$route, 当切换路由时,路由对象会被更新。this.$route 表示当前的路由对象

  1. this.$route.path: 当前路由的路径,会被解析为绝对路径 如:'/home/new.vue'

  2. this.$route.params: 包含路由中的动态片段和全匹配片段的键值对

3.this.$route.router: 路由规则所属的路由器(以及其所属的组件)

  1. this.$route.matched: 包含当前匹配的路径中所包含的所有片段所对应 的配置参数对象

  2. this.$route.name: 当前路径的名字

2. this.$router 全局的路由对象

this.$router 包含了很多属性和方法, 任何页面都可以调用。 $router是一组路由或者说是一种机制,他管理了一组路由$route

*$router具有如下属性
1.push()方法: 这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL。
字符串this.$router.push('home')
对象 this.$router.push({path:'home'})
命名的路由 this.$router.push({name:'user', params:{userId: '123'}})==》/这里的 params 不生效

3.forward()方法,前进一步 this.$router.forward();

4.go()方法 可前进可后退

this.$router.go(-1)后退一步
this.$router.go(2)前进两步,但当步数大于历史记录数,就会无效,是无效,而不是取一个最大值
this.$router.go(0)会刷新页面

上一篇 下一篇

猜你喜欢

热点阅读