vue-router相关总结

作者: xurna | 来源:发表于2019-07-17 15:08 被阅读1次
  • 路由结构为/user/:id时,当用户从/user/foo导航到/user/bar时,相同的组件实例将被重用,它还意味着不会调用组件的生命周期挂钩。要对同一组件中的参数更改做出反应,您只需观察$route对象,或者使用beforerouteupdate
const User = {
  template: '...',
  watch: {
    '$route' (to, from) {
      // react to route changes...
    }
  }
}
// or
const User = {
  template: '...',
  beforeRouteUpdate (to, from, next) {
    // react to route changes...
    // don't forget to call next()
  }
}
  • 基于路径的动态过渡:可以根据目标路由和当前路由之间的关系确定要动态使用的转换:
<!-- use a dynamic transition name -->
<transition :name="transitionName">
  <router-view></router-view>
</transition>
// then, in the parent component,
// watch the `$route` to determine the transition to use
watch: {
  '$route' (to, from) {
    const toDepth = to.path.split('/').length
    const fromDepth = from.path.split('/').length
    this.transitionName = toDepth < fromDepth ? 'slide-right' : 'slide-left'
  }
}
// or 
beforeRouteUpdate (to, from, next) {
    const toDepth = to.path.split('/').length
    const fromDepth = from.path.split('/').length
    this.transitionName = toDepth < fromDepth ? 'slide-right' : 'slide-left'
    next()
},
  • 滚动行为scrollBehavior:需要浏览器支持history.pushState,当使用后退/前进按钮导航时,返回savedposition将去到页面原来定位的位置,以模拟原生的行为。
const router = new VueRouter({
  routes: [...],
  scrollBehavior (to, from, savedPosition) {
    // return desired position
    if (savedPosition) {
        return savedPosition
     } else {
        return { x: 0, y: 0 }
     }
  }
})
  • 优化打包,懒加载路径:如果我们可以将每个路由的组件分割成单独的块,并且只在访问路由时加载它们,那么效率会更高。
  new Router({
      routes: [
        {
          path: '/',
          component: resolve => require(['pages/index.vue'], resolve)
        },
      ]
    })

相关文章

  • vue-router相关总结

    路由结构为/user/:id时,当用户从/user/foo导航到/user/bar时,相同的组件实例将被重用,它还...

  • Vue-router 相关

    一、vue-router 如何传参 1、用name传递参数 步骤: (1)在路由文件src/router/in...

  • vue-router相关

    路由组件传参 比方说不同的用户拥有不同的id,需要进入同一个页面,就可以在vueRouter实例中这样写 用:id...

  • vue+express+mongo之踩坑--Vue-Router

    项目中用到了vue-router,那么就简单总结一下vue-router的使用。 1.vue-router安装 通...

  • vue-router

    关于路由的参考:vue-router总结 https://www.tuicool.com/articles/J3A...

  • 百川平台项目总结

    百川平台项目总结 技术栈:vue + vuex + vue-router + vee-validate + Ele...

  • vue-router

    1. vue-router query 和 params 传参 params 传参: 总结: 用params传参只...

  • vue-router总结

    1、通过 :to 传参 2、通过URL传参 router/index.js App.vue Hi.vue 重定向 ...

  • 使用hash实现页面切换

    最近在看浏览器相关的内容,看到hash和history相关的内容了,自然而然想起来vue-router的两种模式,...

  • Vue练习-后台管理的登录页面

    登录页面制作 相关知识点:vue-router + element-UI + axios 先记录一下,在CLI3自...

网友评论

    本文标题:vue-router相关总结

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