Detailed explanation of the difference between routing hooks in Vue2.x and Vue3.x

Detailed explanation of the difference between routing hooks in Vue2.x and Vue3.x

vue2.x

Pre-concept:

Routing hook categories

There are 3 categories and 7 hooks in total.

The concept of routing and components (for easy understanding of hook functions)

Routing and components are two concepts, which can be roughly considered as:

  • The route is the browser URL
  • Components are different types of content that appear on a web page.

Global routing hooks

router.beforeEach(to, from, next){ } Pre-navigation guard

Before routing

router.afterEach(to, from, next){ } Post navigation guard

After the route enters

router.beforeResolve(to, from, next){ } Resolution guard

When parsing a component, all configurations have been read, and all previous levels have passed. Execute before parsing the component

Routing configuration guard hook

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-iNt9TxY9-1631622860323) (C:\Users\hwm\AppData\Roaming\Typora\typora-user-images\image-20210914193901604.png)]

beforeEnter() is called before reading the routing configuration information

Executed before beforeCreate()

const router = new VueRouter({
  routes: [
    {
      path: '/foo',
      component: Foo,
      beforeEnter: (to, from, next) => { }
    }
  ]
})

Guard hooks in components

beforeRouteEnter(to, from, next){ } before entering the component

By default, you cannot get the component instance this

However, the following methods can be used to access

beforeRouteEnter(to, from, next){ 

  // The callback function of next() is instantiated and executed when entering the route next(vm => {
    // vm is equivalent to the current component this
  })
}

beforeRouteUpdate(to, from, next){ } before modifying the component

The current component is still using the routing address change and will only be executed if the above two conditions are met

beforeRouteLeave(to, from, next){ } Before leaving the component

Routing hook execution order

Eg: jump from component A to component B

beforeRouteLeave() leaves component A first

beforeEach() global - front navigation guard

beforeEnter() before reading routing configuration information (reading B routing information)

beforeRouteEnter() before entering the component

beforeResolve() resolution guard

afterEach() global - post navigation guard

If there is an update on route B, the following three hooks will be executed each time:

beforeEach() Global - Pre-navigation guard

beforeResolve() global - resolution guard

beforeRouteUpdate() in the component - before modifying the component

vue3.x

Vue3.x is only slightly different from vue2.x, so we will only explain the differences here.

Comparison chart

Please add a description of the image

Differences and Supplements:

  • In Vue2.x, there are 3 components. If component A jumps to component B, there are subcomponents in component B:

The subcomponents in component B will not trigger the routing hook

  • In Vue3.x, there are 3 components. If component A jumps to component B, there are subcomponents in component B:

The subcomponents in component B trigger the routing hooks: onBeforeRouteUpdate and onBeforeRouteLeave
The subcomponents in component B will not trigger the routing hook

  • In Vue3.x, there are 3 components. If component A jumps to component B, there are subcomponents in component B:

The subcomponents in component B trigger the routing hooks: onBeforeRouteUpdate and onBeforeRouteLeave

The above is the detailed explanation of the differences between routing hooks in Vue2.x and Vue3.x. For more information about the differences between routing hooks in Vue2.x and Vue3.x, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of vue3 cache page keep-alive and unified routing processing
  • Vue3.0 combined with bootstrap to create a multi-page application
  • Vue3.0 implements the encapsulation of the drop-down menu
  • Vue3+TypeScript implements a complete example of a recursive menu component
  • A simple example of using Vue3 routing VueRouter4
  • Vue2/vue3 routing permission management method example
  • Use of Vue3 pages, menus, and routes

<<:  Linux system (Centos6.5 and above) installation jdk tutorial analysis

>>:  Install Tomcat on Linux system and configure Service startup and shutdown

Recommend

How to Dockerize a Python Django Application

Docker is an open source project that provides an...

IE6 web page creation reference IE6 default style

This is not actually an official document of IE. I...

Steps to deploy Spring Boot project using Docker

Table of contents Create a simple springboot proj...

JavaScript implements draggable modal box

This article shares the specific code of JavaScri...

Hello dialog box design experience sharing

"What's wrong?" Unless you are accus...

WeChat applet custom tabBar step record

Table of contents 1. Introduction 2. Customize ta...

Analysis of Apache's common virtual host configuration methods

1. Apache server installation and configuration y...

MySQL 8.0.25 installation and configuration method graphic tutorial

The latest download and installation tutorial of ...

MySQL transaction autocommit automatic commit operation

The default operating mode of MySQL is autocommit...

Ubuntu basic settings: installation and use of openssh-server

Record the installation and use of openssh-server...

A few front-end practice summaries of Alipay's new homepage

Of course, it also includes some personal experien...

Call and execute host docker operations in docker container

First of all, this post is dedicated to Docker no...

Specific use of CSS front-end page rendering optimization attribute will-change

Preface When scroll events such as scroll and res...