<template> <div id="app"> <transition :name="transitionName"> <router-view class="transitionBody"></router-view> </transition> </div> </template> <script> export default { name: 'app', data() { return { transitionName: 'transitionLeft', }; }, // 监听路由的路径,可以通过不同的路径去选择不同的切换效果 watch: { '$route'(to, from) { const arr = ['/', '/about',] this.transitionName = arr.indexOf(to.path) > arr.indexOf(from.path) ? 'transitionLeft' : 'transitionRight' } } } </script> <style> .transitionBody { transition: all 0.4s ease-out; } .transitionLeft-enter, .transitionRight-leave-active { -webkit-transform: translate(100%, 0); transform: translate(100%, 0); } .transitionLeft-leave-active, .transitionRight-enter { -webkit-transform: translate(-100%, 0); transform: translate(-100%, 0); } .transitionLeft-enter-active, .transitionRight-enter-active { position: absolute; top: 0; right: 0; left: 0; bottom: 0; } </style>
import Vue from "vue"; import VueRouter from "vue-router"; import HomeView from "../views/HomeView.vue"; Vue.use(VueRouter); const routes = [ { path: "/", name: "home", component: HomeView, meta: { index: 1, }, }, { path: "/about", name: "about", component: () => import(/* webpackChunkName: "about" */ "../views/AboutView.vue"), meta: { index: 2, }, }, ]; const router = new VueRouter({ routes, mode: "history", }); export default router;