关于 VUE vue-router的基本使用(2)

一,编程式导航
//如果提供了 path,params 会被忽略,上述例子中的 query 并不属于这种情况。

router.push(location, onComplete?, onAbort?)
//字符串
router.push('home');
//对象
router.push({path:'name'});
//带参数查询路由
router.push({path:'name',query:{id:'3'}});
// 命名的路由
router.push({name:'name',params:{id:'3'}})
router.push(location).then(onComplete).catch(onAbort)
router.replace(location, onComplete?, onAbort?)
router.replace(''home)// 和push方法相同 区别:不会添加记录栈它不会向 history 添加新记录
router.replace(location).then(onComplete).catch(onAbort)
router.go(n)
router.go(1)
router.go(-1)
//这个方法的参数是一个整数,意思是在 history 记录中向前或者后退多少步,类似 window.history.go(n)。
router. forward():前进一步。
router.back():回退一步。

路由对象
一个路由对象 (route object) 表示当前激活的路由的状态信息,包含了当前 URL 解析得到的信息,还有 URL 匹配到的路由记录 (route records)。
路由对象是不可变 (immutable) 的,每次成功的导航后都会产生一个新的对象。
路由对象出现在多个地方:
1.在组件内,即 this.$route
2.在 $route 观察者回调内
3.router.match(location) 的返回值

router.beforeEach((to, from, next) => {
  // `to` 和 `from` 都是路由对象
})
const router = new VueRouter({
  scrollBehavior(to, from, savedPosition) {
    // `to` 和 `from` 都是路由对象
  }
})
//属性
this.$route.path//字符串,对应当前路由的路径,总是解析为绝对路径,如 "/foo/bar"。
this.$route.params //一个 key/value 对象,包含了动态片段和全匹配片段,如果没有路由参数,就是一个空对象。
this.$route.query //一个 key/value 对象,表示 URL 查询参数。例如,对于路径 /foo?user=1,则有 $route.query.user == 1,如果没有查询参数,则是个空对象。
this.$route.hash //当前路由的 hash 值 (带 #) ,如果没有 hash 值,则为空字符串。
this.$route.fullPath //完成解析后的 URL,包含查询参数和 hash 的完整路径。

this.$route.matched //一个数组,包含当前路由的所有嵌套路径片段的路由记录 。路由记录就是 routes 配置数组中的对象副本 (还有在 children 数组)。
this.$route.name //当前路由的名称.
this.$route.redirectedFrom //如果存在重定向,即为重定向来源的路由的名字
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值