Home > Web Front-end > Vue.js > body text

How to use navigation guards in Vue Router?

WBOY
Release: 2023-07-21 20:10:53
Original
1026 people have browsed it

How to use the navigation guard in Vue Router?

Navigation guard is a very important and powerful feature in Vue Router. It allows us to execute some custom logic before navigation is triggered or before leaving the current route. By using navigation guards, we can implement functions such as routing permission verification, page switching animation, etc.

Vue Router provides three types of navigation guards:

  1. Global guards: guards that will be triggered by all routes in the application, including beforeEach, beforeResolve and afterEach.
  2. Route guards: Guards that will only trigger specific routes, including beforeEnter, beforeRouteEnter, beforeRouteUpdate and beforeRouteLeave.
  3. Guards within the component: Only the guards of the current component will be triggered, including beforeRouteEnter, beforeRouteUpdate and beforeRouteLeave.

Let’s explain how to use these navigation guards.

First, we need to introduce Vue Router into the Vue project and create a routing instance. In the process of creating an instance, we can define global guards:

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  routes: [...]
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 在进入每个路由之前执行的逻辑
  next()
})

// 全局解析守卫
router.beforeResolve((to, from, next) => {
  // 在全部组件被解析之后执行的逻辑
  next()
})

// 全局后置守卫
router.afterEach((to, from) => {
  // 在进入每个路由之后执行的逻辑
})
Copy after login

In the above code, we defined three global guards. beforeEach is used to execute logic before entering each route, beforeResolve is used to execute logic after all components are parsed, and afterEach is used to execute logic after entering each route. Using the next() method, you can execute the next guard or perform a routing jump.

Next, we can define routing guards. When creating a route, we can define the guard for each specific route configuration:

import Home from './views/Home.vue'
import About from './views/About.vue'

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home
    },
    {
      path: '/about',
      name: 'about',
      component: About,
      beforeEnter: (to, from, next) => {
        // 在进入指定路由之前执行的逻辑
        next()
      }
    }
  ]
})
Copy after login

In the above code, we define the beforeEnter guard for the /about route. Before entering the route, the function we passed in will be executed. We can write custom logic in the function, and then use the next() method to perform the next guard or route jump.

Finally, we can also define guards inside the component. Inside the component, we can use three types of guards: beforeRouteEnter, beforeRouteUpdate and beforeRouteLeave:

export default {
  ...
  beforeRouteEnter(to, from, next) {
    // 在进入当前组件之前执行的逻辑
    next()
  },
  beforeRouteUpdate(to, from, next) {
    // 当前组件复用时,更新路由参数时执行的逻辑
    next()
  },
  beforeRouteLeave(to, from, next) {
    // 在离开当前组件之前执行的逻辑
    next()
  }
}
Copy after login

The above code shows the usage of guards inside the component. We can write our logic in the corresponding life cycle hook, and then use next() performs the next operation.

To sum up, the navigation guard in Vue Router is a very flexible and powerful function. We can use global guards, routing guards and intra-component guards to implement various customized logic. In daily development, we can flexibly use these guards according to specific needs to achieve better user experience and function implementation.

To sum up, navigation guard is a very important function in Vue Router. It can help us do some custom logic processing during the route switching process. Through global guards, route guards and intra-component guards, we can implement various functions, such as route interception, permission verification, route switching animation, etc. In actual project development, rational use of navigation guards can improve development efficiency and user experience.

The above is the detailed content of How to use navigation guards in Vue Router?. 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