简体   繁体   English

使用Vue-Router转换Vue页面

[英]Vue page transition with Vue-Router

I'm trying to make nice page transitions using Vue and Vue-Router. 我正在尝试使用Vue和Vue-Router进行漂亮的页面转换。

I load different pages (components) directly in the router-view , these have a nice animation. 我直接在router-view中加载不同的页面(组件),这些页面具有很好的动画效果。

I've already got a great animation for all my pages, however I need to figure out how to simply change the transition based on the router-link that is clicked. 我已经为所有页面制作了一个很棒的动画,但是我需要弄清楚如何简单地基于所单击的路由器链接来更改过渡。

If class of route-link is special, do another animation. 如果route-link的类别特殊,请执行另一个动画。

App.vue 应用程序

<template>
  <div id="app">
    <nav>
      <router-link to="/1">Page 1</router-link>
      <router-link to="/2">Page 2</router-link>
      <router-link to="/3">Page 3</router-link>
      <router-link class="special" to="/4">Page 4</router-link>
    </nav>
    <transition @enter="enter" @leave="leave">
      <router-view/>
    </transition>
  </div>
</template>

<script>
  export default {
    ...
    methods: {
      enter(el, done) {
        // main page animation with a timeline using Greensock
      },
      leave(el, done) { 
        // main page animation with a timeline using Greensock
      }
    },
  }
</script>

How do I get this to work? 我该如何工作?

If the user clicks on router-link with a class of special, change 如果用户单击具有特殊类别的路由器链接,请更改

<transition @enter="enter" @leave="leave">
  <router-view/>
</transition>

to: 至:

<transition @enter="enterSpecial" @leave="leaveSpecial">
  <router-view/>
</transition>

so that I can add 这样我可以添加

    methods: {
      enter(el, done) {
        // main page animation with a timeline using Greensock
      },
      leave(el, done) { 
        // main page animation with a timeline using Greensock
      }
    },

I've searched through the docs + googled, and haven't found anything that could help. 我已经搜索了docs + googled,但没有找到任何可以帮助的东西。

You can use Router Meta Fields to know which animation you should apply. 您可以使用“路由器元字段”来了解应该应用哪个动画。 On routes declaration, you can add meta fields for each route. 在路线声明中,您可以为每个路线添加元字段。 Eg: 例如:

const router = new VueRouter({
  routes: [
    {
      path: '/default',
      component: SomeComponent
    },
    {
      path: '/special',
      component: SomeComponent2,
      // meta fields
      meta: { 
          animation: 'special'
      }
    }
  ]
})

Then on your components, you can do this: 然后在您的组件上,可以执行以下操作:

 methods: {
   enter(el, done) {
     if (this.$route.meta.animation === 'special') {
         // apply special animation
     } else {
        // main page animation with a timeline using Greensock
     }
   },
   leave(el, done) { 
     // main page animation with a timeline using Greensock
   }
 }

You can find more information about vue-router meta fields here . 您可以在此处找到有关vue-router元字段的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM