Detailed introduction and demonstration of Vue routing guard

Insert image description here

Vue route guards are a mechanism for controlling route navigation in Vue.js applications. It allows you to execute code before, after, or on specific routes to implement functions such as permission control, data loading, page switching animations, etc. . In the following introduction, I will first provide an official definition and popular explanation, and then detail the global front route guard, global post route guard, exclusive route guard and intra-component route guard, and provide example demonstrations for each concept.

general concept

Official introduction

Vue route guards are a set of callback functions that allow you to intercept and perform additional logic during route navigation. Route guards can be used for global route navigation control, individual route navigation control, and navigation control within components.

Popular explanation

Route guards are like "navigation guards" in your application, they monitor and manage switching between your pages. They can help you do some useful things, such as checking whether the user has permission before entering a page, or adding some animation effects when switching pages.

Global front route guard

concept

The global front route guard is a function executed before route switching. It can be used to check the user's identity, permissions, etc., and abort or continue route navigation when necessary.

Example

router.beforeEach((to, from, next) => {
    
    
  // 检查用户是否登录
  if (!isAuthenticated()) {
    
    
    next('/login'); // 如果未登录,重定向到登录页面
  } else {
    
    
    next(); // 已登录,继续路由导航
  }
});

Global post route guard

concept

The global post route guard is a function executed after route switching. It can be used to handle some tasks related to route navigation, such as data processing after the page is loaded.

Example

router.afterEach((to, from) => {
    
    
  // 记录路由切换日志
  console.log(`${
      
      from.path}${
      
      to.path}`);
});

Exclusive route guard

concept

An exclusive route guard is a guard defined on a specific route that only affects navigation for that route. This allows you to define different navigation logic for different routes.

Example

const router = new VueRouter({
    
    
  routes: [
    {
    
    
      path: '/admin',
      component: Admin,
      beforeEnter: (to, from, next) => {
    
    
        // 检查用户是否是管理员
        if (isAdmin()) {
    
    
          next(); // 是管理员,继续导航
        } else {
    
    
          next('/login'); // 不是管理员,重定向到登录页面
        }
      },
    },
  ],
});

Intra-component route guard

concept

Intra-component route guards are guards defined inside a component that can be used to handle logic related to component-specific route navigation.

Example

export default {
    
    
  beforeRouteEnter(to, from, next) {
    
    
    // 在进入该组件之前执行
    // 可以在这里获取路由参数或数据
    next();
  },
  beforeRouteUpdate(to, from, next) {
    
    
    // 在组件复用时执行
    // 可以处理路由参数或数据的变化
    next();
  },
  beforeRouteLeave(to, from, next) {
    
    
    // 在离开该组件时执行
    // 可以进行一些清理操作或弹出确认提示
    next();
  },
};

Vue Route Guard is a powerful tool that can be used to control and customize route navigation behavior, making your application more flexible and secure. Different types of route guards allow you to handle navigation logic at different levels, from global to component level, providing a wide range of application scenarios. I hope you can learn more and let’s go deeper together! ! !

Guess you like

Origin blog.csdn.net/weixin_53742691/article/details/132783016