vue路由守卫和路由传参详解

  • Post author:
  • Post category:vue


完整导航解析流程

  1. 导航被触发。

  2. 在失活的组件里调用

    beforeRouteLeave

    守卫。

  3. 调用全局的

    beforeEach

    守卫。

  4. 在重用的组件里调用

    beforeRouteUpdate

    守卫 (2.2+)。

  5. 在路由配置里调用

    beforeEnter

  6. 解析异步路由组件。

  7. 在被激活的组件里调用

    beforeRouteEnter

  8. 调用全局的

    beforeResolve

    守卫 (2.5+)。

  9. 导航被确认。

  10. 调用全局的

    afterEach

    钩子。

  11. 触发 DOM 更新。

  12. 调用

    beforeRouteEnter

    守卫中传给

    next

    的回调函数,创建好的组件实例会作为回调函数的参数传入。

  • router.beforeEach:全局前置路由守卫,每次切换之前被调用,可以做权限拦截。
  • router.afterEach:全局后置路由守卫,每次切换之后调用, 可用于切换document.title。
  • router.beforeEnter:独享路由守卫,只有前置,没有后置,写在routes配置项里。
  • router.beforeRouteEnter:组件内路由守卫, 写在组件中,路过路由规则进入该组件时被调用。
  • router.beforeRouteLeave:组件内路由守卫, 写在组件中,路过路由规则离开该组件时被调用。

全局路由守卫

const router = new VueRouter({ ... })

router.beforeEach((to, from, next) => {
  // ...
})


  • to: Route


    : 即将要进入的目标路由对象



  • from: Route


    : 当前导航正要离开的路由



  • next: Function


    : 一定要调用该方法来

    resolve

    这个钩子。执行效果依赖

    next

    方法的调用参数。



    • next()


      : 进行管道中的下一个钩子。如果全部钩子执行完了,则导航的状态就是

      confirmed

      (确认的)。



    • next(false)


      : 中断当前的导航。如果浏览器的 URL 改变了 (可能是用户手动或者浏览器后退按钮),那么 URL 地址会重置到

      from

      路由对应的地址。



    • next('/')

      或者

      next({ path: '/' })


      : 跳转到一个不同的地址。当前的导航被中断,然后进行一个新的导航。你可以向

      next

      传递任意位置对象,且允许设置诸如

      replace: true



      name: 'home'

      之类的选项以及任何用在 router-link



      to prop或 router.push中的选项。



    • next(error)


      : (2.4.0+) 如果传入

      next

      的参数是一个

      Error

      实例,则导航会被终止且该错误会被传递给

      router.onError()

      注册过的回调。

全局解析守卫

router.beforeResolve

全局后置钩子

router.afterEach((to, from) => {
  // ...
})

路由独享的守卫

const router = new VueRouter({
  routes: [
    {
      path: '/foo',
      component: Foo,
      beforeEnter: (to, from, next) => {
        // ...
      }
    }
  ]
})

组件内的守卫

const Foo = {
  template: `...`,
  beforeRouteEnter(to, from, next) {
    // 在渲染该组件的对应路由被 confirm 前调用
    // 不能!获取组件实例 `this`
    // 因为当守卫执行前,组件实例还没被创建
  },
  beforeRouteUpdate(to, from, next) {
    // 在当前路由改变,但是该组件被复用时调用
    // 举例来说,对于一个带有动态参数的路径 /foo/:id,在 /foo/1 和 /foo/2 之间跳转的时候,
    // 由于会渲染同样的 Foo 组件,因此组件实例会被复用。而这个钩子就会在这个情况下被调用。
    // 可以访问组件实例 `this`
  },
  beforeRouteLeave(to, from, next) {
    // 导航离开该组件的对应路由时调用
    // 可以访问组件实例 `this`
  }
}


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) {
  const answer = window.confirm('Do you really want to leave? you have unsaved changes!')
  if (answer) {
    next()
  } else {
    next(false)
  }
}

路由传参

通过query方式传参

this.$router.push({
  path: '/detail',
  query: {
    id: id
  }
})
 
{ // 对应路由配置:
  path: '/detail',
  name: 'Detail',
  component: Detail
}
 
 // 子组件获取参数:
 this.$route.query.id

通过params方式传参

this.$router.push({
  name: 'Detail',
  params: {
    id: id
  }
})
  
// 子组件获取参数:
this.$route.params.id

通过路由地址拼接传参

// 直接在路由地址后面拼接参数
this.$router.push({
  path: `/detail/${id}`,
})

//获取参数
this.$route.params.id


注意事项:

query传参:

  • name和path都行,

  • url会带上参数,

  • 通过this.$route.query获取

params传参:

  • 只能name

  • url不会带上参数,刷新就没有了

  • 通过this.$route.params获取



版权声明:本文为weixin_51855875原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。