How to achieve a fade effect page transition between vue-router defined pages (components)?
相关问题
- Axios OPTIONS instead of POST Request. Express Res
- Vue.js - set slot content programmatically
- Getting Uncaught TypeError: …default is not a cons
- Vue.js computed property loses its reactivity when
- How to remove the Vuetify append-icon from the seq
相关文章
- Best way to dynamically change theme of my Vue.js
- vue-router how to persist navbar?
- How to Properly Use Vue Router beforeRouteEnter or
- Difference between nameFunction() {} and nameFunct
- Vue - best way to reuse methods
- setTimeout() not working called from vueJS method
- How to unbind an array copy in Vue.js
- Pausing and resuming a transition
Wrap
<router-view></router-view>
with<transition name="fade"></transition>
and add these styles:Detailed answer
Assuming you have created your application with vue-cli, e.g.:
Install the router:
If you are not developing your app using vue-cli, make sure to add the vue router the standard way:
You can use e.g.: https://unpkg.com/vue-router/dist/vue-router.js
The CLI has created a backbone application for you, which you can add components to.
1) Create page components
In Vue, components (UI elements) can be nested. A page in your app can be made with a regular Vue component that is considered as the root to other components in that page.
Go to
src/
and createpages/
directory. These page-root components (individual pages) will be put in this directory, while the other components used in the actual pages can be put to the ready-madecomponents/
directory.Create two pages in files called
src/pages/Page1.vue
andsrc/pages/Page2.vue
for starters. Their content will be (edit page numbers respectively):2) Setup routing
Edit the generated
src/main.js
add the required imports:Add a global router usage:
Add a router setup:
The last route just redirects the initial path
/
to/page1
. Edit the app initiation:The whole
src/main.js
example is at the end of the answer.3) Add a router view
Routing is set up by now, just a place where the page components will be rendered according to the router is missing. This is done by placing
<router-view></router-view>
somewhere in the templates, you will want to put it in thesrc/App.vue
's<template>
tag.The whole
src/App.vue
example is at the end of the answer.4) Add fade transition effect between page components
Wrap the
<router-view></router-view>
with a<transition name="fade">
element, e.g.:Vue will do the job here: it will create and insert appropriate CSS classes starting with the name specified through-out the effect's duration, e.g.:
.fade-enter-active
. Now define the effects in App.vue's section:That's it. If you run the app now, e.g. with
npm run dev
, it will automatically display Page 1 with a fade-in effect. If you rewrite the URL to /page2, it will switch the pages with fade-out and fade-in effects.Check out the documentation on routing and transitions for more information.
5) Optional: Add links to pages.
You can add links to particular pages with the
<router-link>
component, e.g.:This automatically gives the links a
router-link-active
class in case they are active, but you can also specify custom classes if you are using e.g. Bootstrap:Files for reference
src/main.js:
src/App.vue:
src/pages/Page1.vue:
src/pages/Page2.vue: