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 install and deploy gitlab server on centos7

I am using centos 7 64bit system here. I have tri...

JS native 2048 game source code sharing (the latest on the Internet)

I have been learning about algorithms recently an...

Setting up VMware vSphere in VMware Workstation (Graphic Tutorial)

VMware vSphere is the industry's leading and ...

How to Install Xrdp Server (Remote Desktop) on Ubuntu 20.04

Xrdp is an open source implementation of Microsof...

Solve the problem after adding --subnet to Docker network Create

After adding –subnet to Docker network Create, us...

Where is mysql data stored?

MySQL database storage location: 1. If MySQL uses...

Nginx proxy axios request and precautions

Preface I recently wrote a small demo. Because I ...

Why MySQL database avoids NULL as much as possible

Many tables in MySQL contain columns that can be ...

Installing MySQL 8.0.12 based on Windows

This tutorial is only applicable to Windows syste...

MySQL replication detailed explanation and simple example

MySQL replication detailed explanation and simple...

CentOS8 - bash: garbled characters and solutions

This situation usually occurs because the Chinese...

Classification of web page color properties

Classification of color properties Any color can ...

CSS hacks \9 and \0 may not work for hacking IE11\IE9\IE8

Every time I design a web page or a form, I am tr...