Home  >  Article  >  Web Front-end  >  Practical tutorial on using Vue routing hooks

Practical tutorial on using Vue routing hooks

php中世界最好的语言
php中世界最好的语言Original
2018-04-14 17:13:581863browse

This time I will bring you a practical tutorial on using Vue routing hooks. What are the precautions when using Vue routing hooks? Here are practical cases, let’s take a look.

1. Routing hook syntax

In the official document of vue-router, routing hook is translated as navigation guard. The following is the content in the document Summary . You can also go to the official website through the portal to read the detailed content

Routing hook

Routing hooks are mainly defined for users to perform some special processing when routing changes. Damn. . What a mouthful.

Generally speaking, vue provides three major types of hooks

1. Global hook
2. Hook exclusive to a certain route
3. Intra-component hook

Three parameters are involved in the three routing hooks. Let’s go directly to the official introduction here

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.
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 before guard:

const router = new VueRouter({ ... })
router.beforeEach((to, from, next) => {
 // ...
 next()
})

Each guard method accepts three parameters:

  1. to: Route: The target route object that is about to enter

  2. from: Route: The route that the current navigation is about to leave

  3. next: Function: Must be called This method resolves this hook. The execution effect depends on the calling parameters of the next method

Note: When using global routing hooks, be sure to call next()!!!

(2). Route exclusive guard (hook within route)

You can define the beforeEnter guard directly on the route configuration:

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

These guards have the same method parameters as global front guards.

(3). Guards within the component (hook within the component)

Finally, you can define the routing navigation guard directly in the routing component:

  1. beforeRouteEnter

  2. beforeRouteUpdate (new in 2.2)

  3. 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. Application scenarios of routing 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 fall into the following three categories:

(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 jumping, or there is a pop-up box, the user 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 information can be saved in the session or Vuex

 beforeRouteLeave (to, from, next) {
  localStorage.setItem(name, content); //保存到localStorage中
  next()
}

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to upload pictures to the server through WeChat applet

##JS implements transparent gradient of navigation bar

The above is the detailed content of Practical tutorial on using Vue routing hooks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn