vue-router - 路由嵌套
发布时间:2018-11-03 编辑:小张博客 查看次数:7389
实际生活中的应用界面,通常由多层嵌套的组件组合而成。同样地,URL 中各段动态路径也按某种结构对应嵌套的各层组件,借助 vue-router,使用嵌套路由配置,就可以很简单地表达这种关系。
<div class="col-4"> <!--- 导航 --> <div class="list-group mb-5"> <router-link tag="li" class="nav-link" :to="{name:'history1'}"> <a class="list-group-item list-group-item-action">历史订单</a> </router-link> <router-link tag="li" class="nav-link" :to="{name:'Contact'}"> <a class="list-group-item list-group-item-action">联系方式</a> </router-link> <router-link tag="li" class="nav-link" :to="{name:'OrderingGuide'}"> <a class="list-group-item list-group-item-action">点餐文档</a> </router-link> <router-link tag="li" class="nav-link" :to="{name:'Delivery'}"> <a class="list-group-item list-group-item-action">快递信息</a> </router-link> </div> </div> <div class="col-8"> <!--- <router-view> 渲染路由匹配到的组件---> <router-view></router-view> </div>
要在嵌套的出口中渲染组件,需要在 VueRouter 的参数中使用 children 配置:
const router = new VueRouter({ mode:"history", routes :[ { path:"/",name:'h',component:Home} , { path:"/About",redirect:"/Contact",component:About,children:[ { path:"/about/history1",name:'history1',component:History1 }, { path:"/OrderingGuide",name:'OrderingGuide',component:OrderingGuide }, { path:"/Contact",name:'Contact',component:Contact } ] }, { path:"/Login",component:Login }, { path:"/Register",component:Register }, { path:"/Admin",component:Admin }, { path:"*",redirect:"/"}, ] })
你会发现,children 配置就是像 routes 配置一样的路由配置数组,所以呢,你可以嵌套多层路由。
此时,基于上面的配置,当你访问 /about/history2 时,About 的出口是不会渲染任何东西,这是因为没有匹配到合适的子路由。
Vue 导航守卫
正如其名,vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航。有多种机会植入路由导航过程中:全局的, 单个路由独享的, 或者组件级的。
记住参数或查询的改变并不会触发进入/离开的导航守卫。你可以通过观察 $route 对象来应对这些变化,或使用 beforeRouteUpdate 的组件内守卫。
导航解析流程
导航被触发。
在失活的组件里调用离开守卫。
调用全局的 beforeEach 守卫。
在重用的组件里调用 beforeRouteUpdate 守卫 (2.2+)。
在路由配置里调用 beforeEnter。
解析异步路由组件。
在被激活的组件里调用 beforeRouteEnter。
调用全局的 beforeResolve 守卫 (2.5+)。
导航被确认。
调用全局的 afterEach 钩子。
触发 DOM 更新。
用创建好的实例调用 beforeRouteEnter 守卫中传给 next 的回调函数。
全局守卫
你可以使用 router.beforeEach 注册一个全局前置守卫:
const router = new VueRouter({ ... }) router.beforeEach((to, from, next) => { alert('您好,世界!'); })
当一个导航触发时,全局前置守卫按照创建顺序调用。守卫是异步解析执行,此时导航在所有守卫 resolve 完之前一直处于 等待中。
每个守卫方法接收三个参数:
to: Route: 即将要进入的目标 路由对象
from: Route: 当前导航正要离开的路由
next: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。
1、next(): 进行管道中的下一个钩子。如果全部钩子执行完了,则导航的状态就是 confirmed (确认的)。
2、next(false): 中断当前的导航。如果浏览器的 URL 改变了 (可能是用户手动或者浏览器后退按钮),那么 URL 地址会重置到 from 路由对应的地址。
3、next('/') 或者 next({ path: '/' }): 跳转到一个不同的地址。当前的导航被中断,然后进行一个新的导航。你可以向 next 传递任意位置对象,且允许设置诸如 replace: true、name: 'home' 之类的选项以及任何用在 router-link 的 to prop 或 router.push 中的选项。
4、next(error): (2.4.0+) 如果传入 next 的参数是一个 Error 实例,则导航会被终止且该错误会被传递给 router.onError() 注册过的回调。
确保要调用 next 方法,否则钩子就不会被 resolved。
全局解析守卫
在 2.5.0+ 你可以用 router.beforeResolve 注册一个全局守卫。这和 router.beforeEach 类似,区别是在导航被确认之前,同时在所有组件内守卫和异步路由组件被解析之后,解析守卫就被调用。
全局后置钩子
可以注册全局后置钩子,然而和守卫不同的是,这些钩子不会接受 next 函数也不会改变导航本身:
router.afterEach((to, from) => { // ... })
路由独享的守卫
可以在路由配置上直接定义 beforeEnter 守卫:
const router = new VueRouter({ mode:"history", routes :[ { path:"/",name:'h',component:Home} , { path:"/Login",component:Login }, { path:"/Register",component:Register }, { path:"/Admin",component:Admin, beforeEnter: (to, from, next) => { alert('您还没有登录,请先登录!'); next('/Login'); }}, { path:"*",redirect:"/"}, ] })
组件内的守卫
最后,你可以在路由组件内直接定义以下路由导航守卫:
beforeRouteEnter
beforeRouteUpdate (2.2 新增)
beforeRouteLeave
export default{ beforeRouteEnter (to, from, next) { // 在渲染该组件的对应路由被 confirm 前调用 // 不!能!获取组件实例 `this` // 因为当守卫执行前,组件实例还没被创建 alert('Enter'); }, beforeRouteLeave (to, from, next) { // 导航离开该组件的对应路由时调用 // 可以访问组件实例 `this` if(confirm('你确定要离开吗?')){ next() }else{ next(false) } } }
beforeRouteEnter 守卫 不能 访问 this,因为守卫在导航确认前被调用,因此即将登场的新组件还没被创建。不过,你可以通过传一个回调给 next来访问组件实例。在导航被确认的时候执行回调,并且把组件实例作为回调方法的参数。
beforeRouteEnter (to, from, next) { next(vm => { // 通过 `vm` 访问组件实例 }) }
注意 beforeRouteEnter 是支持给 next 传递回调的唯一守卫。对于 beforeRouteUpdate 和 beforeRouteLeave 来说,this 已经可用了,所以不支持传递回调,因为没有必要了。
beforeRouteUpdate (to, from, next) { // just use `this` this.name = to.params.name next() }
这个离开守卫通常用来禁止用户在还未保存修改前突然离开。该导航可以通过 next(false) 来取消。
beforeRouteLeave (to, from, next) { // 导航离开该组件的对应路由时调用 // 可以访问组件实例 `this` if(confirm('你确定要离开吗?')){ next() }else{ next(false) } }
Copyright © 小张个人博客 All Rights Reserved 渝ICP备15006773号-1
联系方式:[email protected] | 本站文章仅供学习和参考