首页之约随笔-生活工作点滴有些文章不一定是为了上首页投稿

vue-router相关总结

2019-07-17  本文已影响1人  xurna
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()
},
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)
        },
      ]
    })
上一篇 下一篇

猜你喜欢

热点阅读