vue-router
  • Welcome
  • 起步
  • 动态路由匹配
  • 路由嵌套
  • 命名路由
  • 声明式与编程式
  • 命名视图
  • 重定向和别名
  • 路由组件传参
  • 路由模式
  • 路由元信息
  • 过渡动效
  • 数据获取
  • 滚动行为
  • 路由懒加载
  • 路由守卫
Powered by GitBook
On this page
  • Basic
  • Advance

过渡动效

给路由切换时添加过渡效果

Basic

<router-view> 是基本的动态组件,所以我们可以用 <transition> 组件给它添加一些过渡效果,如果你想让每个路由组件有各自的过渡效果,可以在各路由组件内使用 <transition> 并设置不同的 name:

const Foo = {
  template: `
    <transition name="slide">
      <div class="foo">...</div>
    </transition>
  `
}

const Bar = {
  template: `
    <transition name="fade">
      <div class="bar">...</div>
    </transition>
  `
}

Advance

还可以基于当前路由与目标路由的变化关系,动态设置过渡效果:

<!-- 使用动态的 transition name -->
<transition :name="transitionName">
  <router-view></router-view>
</transition>
// 接着在父组件内
// watch $route 决定使用哪种过渡
watch: {
  '$route' (to, from) {
    const toDepth = to.path.split('/').length
    const fromDepth = from.path.split('/').length
    this.transitionName = toDepth < fromDepth ? 'slide-right' : 'slide-left'
  }
}

Previous路由元信息Next数据获取

Last updated 6 years ago