Detailed explanation of Vue router routing guard

Detailed explanation of Vue router routing guard

1. Global beforeEach

When the name stored in Local Storage is zhangsan, the message content will be displayed only when the message is clicked

1. Global beforeEach

Add a routing guard to the router beforeEach

grammar:

router.beforeEach((to, from, next)=>{})

Function: Call the function in beforeEach during initialization and before each route switch

parameter:

1.to: target route

2.from: the route before the jump

3.next: release

2. Implementation

2. Requirement 2

If there are many paths that need to be judged before jumping, you need to write a lot of judgment code, and the judgment structure will be very complicated.

At this time, you can put a meta tag in the route that needs to be judged.

The meta tag provides metadata about the HTML document (metadata refers to data used to describe data)

At this time, you can use the tag field in meta to make a judgment

3. Global post-guard afterEach

The usage of afterEach and beforeEach is basically the same.

The difference is that afterEach does not have the next parameter

1. Change the title to your own title

Assign each route its own title name

const router = new VueRouter({
  routes: [
    {
      name: 'home',
      path: '/home',
      component: Home,
      meta: {
        title: "Home"
      },
      children: [ 
        {
          name: "xiaoxi",
          path: 'message',
          component: Message,
          meta: {
            title: "News"
          },
          children: [
            {
              name: 'xiangqing',
              path: 'detail',
              component: Detail,
              meta: {
                isAuth: true,
                title: "Message Details"
              }
            }
          ]
        }
      ]
    },
    {
      path: '/about',
      component: About,
      meta: {
        title: "About"
      }
    }
  ]
})

Modify the title name in beforeEach

router.afterEach((to, from) => {
  document.title = to.meta.title || "Test"
})

4. Guarding within components

1. beforeRouteEnter

Called when entering this component through routing rules

2. beforeRouteLeave

Called when leaving the component through routing rules

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Specific use of routing guards in Vue
  • Vue2.0 implements navigation guard (route guard)
  • Detailed explanation of Vue's hook functions (route navigation guards, keep-alive, life cycle hooks)
  • Detailed explanation of Vue's routing guard and keep-alive life cycle

<<:  How to stop CSS animation midway and maintain the posture

>>:  A Guide to Optimizing High-Performance Websites

Recommend

Additional instructions for using getters and actions in Vuex

Preliminary Notes 1.Differences between Vue2.x an...

CentOS 6 uses Docker to deploy Zookeeper operation example

This article describes how to use docker to deplo...

How to install JDK8 on Windows

1. Download: http://www.oracle.com/technetwork/ja...

VMware workstation 12 install Ubuntu 14.04 (64 bit)

1. Installation Environment Computer model: Lenov...

Implementation of mysql decimal data type conversion

Recently, I encountered a database with the follo...

React+ts realizes secondary linkage effect

This article shares the specific code of React+ts...

Explanation of the basic syntax of Mysql database stored procedures

drop procedure sp_name// Before this, I have told...

Details on how to use class styles in Vue

Table of contents 1. Boolean 2. Expression 3. Mul...

Native JS to implement breathing carousel

Today I will share with you a breathing carousel ...

Semantics, writing, and best practices of link A

The semantics, writing style, and best practices ...

Facebook's nearly perfect redesign of all Internet services

<br />Original source: http://www.a-xuan.cn/...

A brief discussion on the magical uses of CSS pseudo-elements and pseudo-classes

CSS plays a very important role in a web page. Wi...

Introduction to the usage of exists and except in SQL Server

Table of contents 1. exists 1.1 Description 1.2 E...

A Deep Dive into the MySQL InnoDB Storage Engine

Preface In MySQL, InnoDB belongs to the storage e...