美文网首页前端秘境
vue-router导航钩子

vue-router导航钩子

作者: Rose_yang | 来源:发表于2017-08-16 15:52 被阅读5882次

用到vue-router的导航钩子的时候,发现有三类:

1 、全局导航钩子

beforeEach
beforeResolve
afterEach

2 、某个路由独享的导航钩子

beforeEnter

3 、路由组件上的导航钩子

beforeRouteEnter
beforeRouteUpdate (2.2 新增)
beforeRouteLeave

下面来举例说明一下区别

全局导航钩子

const router = new VueRouter({
    mode: 'history',  // 路由使用history模式
    routes,
    linkActiveClass: 'active',  // 设置当前路由的className
});

// 全局导航钩子 直接挂载在router实例上的

router.beforeEach((to, from, next) => {
    document.title = to.meta.title || 'demo'
    if (!to.query.url && from.query.url) {
        to.query.url = from.query.url
    }
    next()
})

router.afterEach(route => {

})

某个路由独享的导航钩子

{ 
        path: '/',
        component: App,
        children: [
            {
                path: 'apply',
                name: 'apply',
                component: Apply,
                children: [
                    {
                        path: 'launch',
                        name: 'apply-launch-list',
                        component: applyList,

                        // 直接在router的路由配置中使用

                        beforeEnter (to, from, next) {
                            // 如果isBack为true时,证明是用户点击了回退,执行slide-right动画
                            let isBack = this.$store.state.transferBack;

                            if (isBack) {
                                this.transitionName = 'slide-right';
                            } else {
                                this.transitionName = 'slide-left';
                            }
                            // 做完回退动画后,要设置成前进动画,否则下次打开页面动画将还是回退
                            this.$router.isBack = false;
                            next()
                        }
                    },
                    {
                        path: 'draft',
                        name: 'apply-draft-list',
                        component: draftList
                    }
                ]
            }
        ]
}

路由组件上的导航钩子

// 定义一个vue模板,这个模板被router的配置文件的component使用

const Qux = {
  data () {
    return {
      msg: null
    }
  },
  template: `<div>{{ msg }}</div>`,

  // 路由组件上的导航钩子beforeRouteEnter 

  beforeRouteEnter (to, from, next) {
    setTimeout(() => {
      next(vm => {
        vm.msg = 'Qux'
      })
    }, 300)
  }
}


const router = new VueRouter({
  mode: 'history',
  base: __dirname,
  routes: [
    { path: '/', component: Home },

  // 某个路由独享的导航钩子beforeEnter
    { path: '/foo', component: Foo, beforeEnter: guardRoute },

    { path: '/bar', component: Bar, meta: { needGuard: true }},

    // 在这里使用Qux

    { path: '/qux', component: Qux }, 

    { path: '/qux-async', component: resolve => {
      setTimeout(() => {
        resolve(Qux)
      }, 0)
    } },

    { path: '/quux/:id', component: Quux }
  ]
})

其他说明请看Vue-router文档

相关文章

  • vue-router路由守卫

    vue-router导航钩子 vue-router导航钩子的主要作用是拦截导航,让路由完成跳转或者取消; 导航钩子...

  • vue-router导航钩子

    用到vue-router的导航钩子的时候,发现有三类: 1 、全局导航钩子 beforeEachbeforeRes...

  • vue-router 2.0 常用基础知识点之导航钩子

    导航钩子 vue-router 提供的导航钩子主要用来拦截导航,让它完成跳转或取消。有多种方式可以在路由导航发生时...

  • 面试题

    vuex工作流程 vue-router导航守卫(钩子函数) 概念:导航发生变化时,导航钩子主要用来拦截导航,让它完...

  • vue-router 2.0 钩子函数

    vue-router 提供的导航钩子主要用来拦截导航,让它完成跳转或取消。有多种方式可以在路由导航发生时执行钩子:...

  • 2021-04-11

    vue-router 路由钩子函数(导航守卫) 路由钩子函数有三种: 全局钩子: beforeEach(全局前置守...

  • Vue面试锦集

    1.Vue有哪几种导航钩子vue-router 提供的导航钩子主要用来拦截导航,让它完成跳转或取消。1.全局的 b...

  • Vue-Router面试题

    一、vue-router有几种钩子函数?具体是什么及其参数 1、全局路由。(全局导航钩子主要有两种钩子:前置守...

  • 详解Vue路由钩子及应用场景

    一. 路由钩子语法 在vue-router的官方文档中, 将路由钩子翻译为导航守卫, 下面是文档中的内容摘要, 大...

  • vue组件级路由钩子函数介绍,及实际应用

    正如其名,vue-router提供的导航钩子主要用来拦截导航,让它完成跳转或取消。 有多种方式可以在路由导航发生时...

网友评论

    本文标题:vue-router导航钩子

    本文链接:https://www.haomeiwen.com/subject/yuxirxtx.html