Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the navigation hook of vue-router

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use the navigation hook of vue-router". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the navigation hook of vue-router".

There are seven hook functions in vue-router, which are mainly divided into three categories:

Global hook

Front guard: router.beforeEach

Rear guard: router.afterEach

Global resolution guard: router.beforeResolve

Note: the global resolution guard (router.beforeResolve) is called after the guard and asynchronous routing components are parsed within all components (before the navigation is confirmed).

Routing exclusive hook

Routing exclusive hook: a navigation hook that is exclusive to a single route and is directly defined on the routing configuration.

BeforeEnter (routing exclusive guard)

Const router = new VueRouter ({

Routes: [

{

Path: "/ setting"

Component: () = > import ("@ / components/setting.vue")

BeforeEnter: (to, from, next) = > {

Console.log ("beforeEnter")

/ / do something

Next ()

}

}

]

})

The hook function takes three parameters: to, from, and next ()

The routing object that to:router is about to enter

From: the route that the current navigation is about to leave

Next (): is a function that performs a hook in the pipeline. If it is finished, the navigation status is confirmed (confirmed), otherwise it is false, terminating the navigation.

In-component navigation hook

BeforeRouteEnter: execution time = = > render the corresponding route of this component before it is confirm

BeforeRouteUpdate: execution time = > when the current route has changed, but the component is still rendered

BeforeRouteLeave: execution time = = > when navigation leaves the corresponding route for this component

These three navigation hooks are defined directly within the routing component.

Const File = {

Template:--File file--

BeforeRouteEnter (to, from, next) {

/ / … Do something

}

BeforeRouteUpdate (to, from, next) {

/ / … Do something

}

BeforeRouteLeave (to, from, next) {

/ / … Do something

}

}

Thank you for your reading, the above is the content of "how to use the navigation hook of vue-router". After the study of this article, I believe you have a deeper understanding of how to use the navigation hook of vue-router, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report