Home > Article > Web Front-end > Detailed explanation of examples of Vue routing hooks and application scenarios
This article mainly introduces the detailed explanation of Vue routing hooks and application scenarios (summary). The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.
1. Routing hook syntax
In the official document of vue-router, routing hook is translated as navigation guard. The following is a summary of the content in the document. You can also go to the official website through the portal. Read details
Routing hook
Routing hook is mainly defined for users to perform some special processing when routing changes, rely on it. . What a mouthful.
Generally speaking, Vue provides three major types of hooks
1, global hooks
2, hooks exclusive to a certain route
3, In-component hooks
All three routing hooks involve three parameters. Let’s go directly to the official introduction here
to: Route: The target routing object that is about to enter
from: Route: The route that the current navigation is about to leave
next: Function: This method must be called to resolve this hook. The execution effect depends on the calling parameters of the next method.
next(): Proceed to the next hook in the pipeline. If all hooks are executed, the navigation status is confirmed.
next(false): Interrupt current navigation. If the browser's URL changes (perhaps manually by the user or by the browser's back button), the URL address will be reset to the address corresponding to the from route.
next(‘/’) or next({ path: ‘/’ }): Jump to a different address. The current navigation is interrupted and a new navigation is started.
(1). Global guard (global routing hook)
You can use router.beforeEach to register a global pre-guard:
const router = new VueRouter({ ... }) router.beforeEach((to, from, next) => { // ... next() })
Each guard method accepts three Parameters:
to: Route: The target route object that is about to enter
from: Route: The route that the current navigation is about to leave
next: Function: This method must be called to resolve this hook. The execution effect depends on the calling parameters of the next method
Note: When using the global routing hook, be sure to call next()!!!
(2). Exclusive to routing Guards (in-route hooks)
You can directly define beforeEnter guards in the routing configuration:
const router = new VueRouter({ routes: [ { path: '/foo', component: Foo, beforeEnter: (to, from, next) => { // ... } } ] })
The method parameters of these guards are the same as those of global front guards.
(3). Guards within the component (hook within the component)
Finally, you can directly define the routing navigation guard in the routing component:
beforeRouteEnter
beforeRouteUpdate (new in 2.2)
beforeRouteLeave
const Foo = { template: `...`, beforeRouteEnter (to, from, next) { // 在渲染该组件的对应路由被 confirm 前调用 // 不!能!获取组件实例 `this` // 因为当守卫执行前,组件实例还没被创建 }, beforeRouteUpdate (to, from, next) { // 在当前路由改变,但是该组件被复用时调用 // 举例来说,对于一个带有动态参数的路径 /foo/:id,在 /foo/1 和 /foo/2 之间跳转的时候, // 由于会渲染同样的 Foo 组件,因此组件实例会被复用。而这个钩子就会在这个情况下被调用。 // 可以访问组件实例 `this` }, beforeRouteLeave (to, from, next) { // 导航离开该组件的对应路由时调用 // 可以访问组件实例 `this` } }
2. Routing Application scenarios of hooks in actual development
Routing hooks are rarely used in the actual development process. In actual projects, I have only used beforeRouteLeave within components. The usage scenarios are as follows:
(1) Clear the timer in the current component
When there is a timer in a component, when the route is switched, you can use beforeRouteLeave to clear the timer to avoid occupying memory. :
beforeRouteLeave (to, from, next) { window.clearInterval(this.timer) //清楚定时器 next() }
(2) When there are unclosed windows or unsaved content on the page, prevent the page from jumping
If there is important information in the page that requires the user to save it before proceeding Jump, or there is a pop-up box. Users should be prevented from jumping
beforeRouteLeave (to, from, next) { //判断是否弹出框的状态和保存信息与否 if (this.dialogVisibility === true) { this.dialogVisibility = false //关闭弹出框 next(false) //回到当前页面, 阻止页面跳转 }else if(this.saveMessage === false) { alert('请保存信息后退出!') //弹出警告 next(false) //回到当前页面, 阻止页面跳转 }else { next() //否则允许跳转 } }
(3) Save relevant content to Vuex or Session
When the user needs to close the page, the public The information is saved to session or Vuex
beforeRouteLeave (to, from, next) { localStorage.setItem(name, content); //保存到localStorage中 next() }
Related recommendations:
Detailed explanation of vue-router routing and inter-page navigation
Vue login route verification implementation method
Detailed explanation of route verification and corresponding interception usage in vue
The above is the detailed content of Detailed explanation of examples of Vue routing hooks and application scenarios. For more information, please follow other related articles on the PHP Chinese website!