Home > Web Front-end > Vue.js > How to use routing to customize page switching animation effects in a Vue project?

How to use routing to customize page switching animation effects in a Vue project?

WBOY
Release: 2023-07-21 14:37:48
Original
2906 people have browsed it

How to use routing to customize the page switching animation effect in the Vue project?

Introduction:
In the Vue project, routing is one of the functions we often use. Switching between pages can be achieved through routing, providing a good user experience. In order to make page switching more vivid, we can achieve it by customizing animation effects. This article will introduce how to use routing to customize the page switching animation effect in the Vue project.

  1. Create Vue project
    First, we need to create a Vue project. You can use Vue CLI to quickly build a basic Vue project. The command is as follows:

    vue create project-name
    Copy after login
  2. Install Vue Router
    Install Vue Router in the project, you can install it through the following command:

    npm install vue-router
    Copy after login
  3. Customized page switching animation effect
    In the Vue project, page switching can be achieved through the <transition> component provided by Vue Router animation effects. We can achieve customized page switching animation effects by wrapping a <transition> component outside the <router-view> component and setting the class name of the animation effect. The specific steps are as follows:
  • Introduce Vue Router and create a routing instance in the project’s entry file main.js:

    import VueRouter from 'vue-router'
    import App from './App.vue'
    
    Vue.use(VueRouter)
    
    const router = new VueRouter({
    routes: [
      // 配置路由规则
    ]
    })
    
    new Vue({
    router,
    render: h => h(App),
    }).$mount('#app')
    Copy after login
  • Use <router-view> in the root component of the project App.vue to display the components corresponding to the current route:

    <template>
    <div id="app">
      <transition name="fade" mode="out-in">
        <router-view></router-view>
      </transition>
    </div>
    </template>
    Copy after login
  • Define the fade animation effect in the style file of App.vue:

    .fade-enter-active, .fade-leave-active {
    transition: opacity 0.5s;
    }
    
    .fade-enter, .fade-leave-to {
    opacity: 0;
    }
    Copy after login

In this way, when the route switches, the page The transition animation of the fade effect will be displayed.

  1. Customize different page switching animation effects
    If you need to customize different switching animation effects for different pages, you can set the meta field in the routing configuration, in the component Read this field in to dynamically set the class value of the animation effect. The specific steps are as follows:
  • Set the meta field in the routing configuration:

    const router = new VueRouter({
    routes: [
      { 
        path: '/page1', 
        component: Page1, 
        meta: { transition: 'slide' } 
      },
      { 
        path: '/page2', 
        component: Page2, 
        meta: { transition: 'fade' } 
      },
    ]
    })
    Copy after login
  • at In App.vue, set the class name of the animation effect according to the meta field of the route:

    <template>
    <div id="app">
      <transition :name="transitionName" mode="out-in">
        <router-view></router-view>
      </transition>
    </div>
    </template>
    
    <script>
    export default {
    data() {
      return {
        transitionName: 'fade'  // 默认动画效果
      }
    },
    watch: {
      $route(to, from) {
        // 根据路由的meta字段设置动画效果的class名
        this.transitionName = to.meta.transition || 'fade';  
      }
    }
    }
    </script>
    Copy after login
  • In this way, every time the route is switched, it will be based on the route The meta field is used to dynamically set the animation effect of page switching.

    Summary:
    By using the <transition> component of Vue Router and the meta field of the route, we can easily customize the animation effect of page switching. . In this way, we can add different animation effects to different page switches to improve user experience. I hope this article will help you understand how to use routing to customize page switching animation effects in Vue projects.

    The above is the detailed content of How to use routing to customize page switching animation effects in a Vue project?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template