From: Eduardo San Martin Morote Date: Mon, 31 Aug 2020 16:58:02 +0000 (+0200) Subject: docs: transitions X-Git-Tag: v4.0.0-beta.8~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2b7300ba2b48e5a2f3729deff77a53cf9ecb4838;p=thirdparty%2Fvuejs%2Frouter.git docs: transitions --- diff --git a/docs/guide/advanced/transitions.md b/docs/guide/advanced/transitions.md index 9d525525..0cde4b79 100644 --- a/docs/guide/advanced/transitions.md +++ b/docs/guide/advanced/transitions.md @@ -1,60 +1,59 @@ # Transitions -Since the `` is essentially a dynamic component, we can apply transition effects to it the same way using the `` component: +In order to use transitions on your route components and animate navigations, you need to use the [v-slot API](../../api#v-slot): ```html - - - - - + + + + + ``` [All transition APIs](https://vuejs.org/guide/transitions.html) work the same here. ## Per-Route Transition -The above usage will apply the same transition for all routes. If you want each route's component to have different transitions, you can instead use `` with different names inside each route component: +The above usage will apply the same transition for all routes. If you want each route's component to have different transitions, you can instead combine [meta fields](./meta.md) and a dynamic `name` on ``: ```js -const Foo = { - template: ` - -
...
-
- ` -} - -const Bar = { - template: ` - -
...
-
- ` -} +const routes = [ + { path: '/custom-transition', meta: { transition: 'slide-left' } }, + { path: '/other-transition', meta: { transition: 'slide-right' } }, +] +``` + +```html + + + + + + ``` ## Route-Based Dynamic Transition -It is also possible to determine the transition to use dynamically based on the relationship between the target route and current route: +It is also possible to determine the transition to use dynamically based on the relationship between the target route and current route. Using a very similar snippet to the one just before: ```html - - - + + + + + ``` +We can add an [after navigation hook](./navigation-guards.md#global-after-hooks) to dynamically add information to the `meta` field based on the depth of the route + ```js -// 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' - } -} +router.afterEach((to, from) => { + const toDepth = to.path.split('/').length + const fromDepth = from.path.split('/').length + to.meta.transitionName = toDepth < fromDepth ? 'slide-right' : 'slide-left' +}) ``` -See full example [here](https://github.com/vuejs/vue-router/blob/dev/examples/transitions/app.js). + + diff --git a/playground/App.vue b/playground/App.vue index 62aec5f3..20e7de8b 100644 --- a/playground/App.vue +++ b/playground/App.vue @@ -159,22 +159,23 @@ - + diff --git a/playground/index.html b/playground/index.html index f2874ec9..396abe5d 100644 --- a/playground/index.html +++ b/playground/index.html @@ -20,7 +20,7 @@ } .fade-enter-active, .fade-leave-active { - transition: opacity 0.15s ease; + transition: opacity 0.3s ease; } .fade-enter-from, .fade-leave-to { @@ -30,6 +30,12 @@ position: absolute; transition: all 0.3s cubic-bezier(0.55, 0, 0.1, 1); } + .slide-left-enter-active, + .slide-left-leave-active, + .slide-right-enter-active, + .slide-right-leave-active { + transition: all 0.3s; + } .slide-left-enter-from, .slide-right-leave-to { opacity: 0; diff --git a/playground/router.ts b/playground/router.ts index 924dccb1..6e6e7138 100644 --- a/playground/router.ts +++ b/playground/router.ts @@ -14,6 +14,7 @@ import GuardedWithLeave from './views/GuardedWithLeave.vue' import ComponentWithData from './views/ComponentWithData.vue' import { globalState } from './store' import { scrollWaiter } from './scrollWaiter' +import RepeatedParams from './views/RepeatedParams.vue' let removeRoute: (() => void) | undefined export const routerHistory = createWebHistory() @@ -42,6 +43,7 @@ export const router = createRouter({ { path: '/long-:n', name: 'long', component: LongView }, { path: '/lazy', + meta: { transition: 'slide-left' }, component: async () => { await delay(500) return component() @@ -77,7 +79,7 @@ export const router = createRouter({ ], }, { path: '/with-data', component: ComponentWithData, name: 'WithData' }, - { path: '/rep/:a*', component: component, name: 'repeat' }, + { path: '/rep/:a*', component: RepeatedParams, name: 'repeat' }, { path: '/:data(.*)', component: NotFound, name: 'NotFound' }, { path: '/nested', @@ -189,6 +191,14 @@ router.beforeEach((to, from, next) => { next() }) +router.afterEach((to, from) => { + if (to.name === from.name && to.name === 'repeat') { + const toDepth = to.path.split('/').length + const fromDepth = from.path.split('/').length + to.meta.transition = toDepth < fromDepth ? 'slide-right' : 'slide-left' + } +}) + router.afterEach((to, from) => { // console.log( // `After guard: from ${from.fullPath} to ${ diff --git a/playground/views/RepeatedParams.vue b/playground/views/RepeatedParams.vue new file mode 100644 index 00000000..cafdb2d5 --- /dev/null +++ b/playground/views/RepeatedParams.vue @@ -0,0 +1,39 @@ + + +