参看:

Vue导航守卫beforeRouteEnter,beforeRouteUpdate,beforeRouteLeave详解

vue组件独享守卫钩子函数参数详解(beforeRouteEnter、beforeRouteUpdate、beforeRouteLeave)

路由守卫beforeEach,beforeResolve,afterEach

组件中的beforeRoute函数

// 组件在加载时调用,此时组件实例还没有被加载
beforeRouteEnter(to, from, next) {
console.log(this, 'beforeRouteEnter') // undefined
console.log(to, 'to')
console.log(from, 'form')
console.log(next, 'next()')
next(vm => {
// 因为当钩子执行前,组件实例还没被创建
// vm 就是当前组件的实例相当于上面的 this,所以在 next 方法里你就可以把 vm 当 this 来用了。
console.log(vm) // 当前组件的实例
})
},
beforeRouteUpdate(to, from, next) {
// 在当前路由改变,但是该组件被复用时调用
// 对于一个带有动态参数的路径 /good/:id,在 /good/1 和 /good/2 之间跳转的时候,
// 由于会渲染同样的good组件,因此组件实例会被复用。而这个钩子就会在这个情况下被调用。
// 可以访问组件实例 `this`
console.log(this, 'beforeRouteUpdate') // 当前组件实例
console.log(to, 'to')
console.log(from, 'from')
console.log(next, 'next()')
next()
},
beforeRouteLeave(to, from, next) {
// 导航离开该组件的对应路由时调用
// 可以访问组件实例 `this`
console.log(this, 'beforeRouteLeave') // 当前组件实例
console.log(to, 'to')
console.log(from, 'from')
console.log(next, 'next()')
next()
},

全局中的before函数

beforeEach

import Vue from 'vue'
import Router from 'vue-router' import Son1 from '@/view/New/son1.vue'
import Son2 from '@/view/New/son2.vue'
import Son3 from '@/view/New/son3.vue' Vue.use(Router)
const VUEROUTER = new Router({
routes: [
{ path: '/', redirect: '/son1' },
{ path: 'son1', name: "son1", component: Son1 },
{ path: 'son2', name: "son2", component: Son2 },
{ path: 'son3', name: "son3", component: Son3 }
]
}) // 为路由对象,添加 导航守卫:
// beforeEach 方法,会在每个路由规则,被匹配之前,做一件事情
VUEROUTER.beforeEach((to, from, next) => {
console.log('to',to)
console.log('from', from)
console.log('next', next)
next()
}) export default VUEROUTER

通过beforeEach这个方法,可以

1、验证用户是否登录

2、用户权限;

3、用户输入的路路径是否存在,需要重定向到哪个页面

beforeResolve 和 afterEach

router.beforeEach((to, from, next) => {
console.log('beforeEach')
next()
})
router.beforeResolve((to, from, next) => {
console.log('beforeResolve')
next()
})
router.afterEach((to, from) => {
console.log('afterEach')
})

进入一个页面触发的构造函数:

beforeEach => beforeRouteEnter => beforeResolve => afterEach => created => mounted => beforeRouteLeave

Vue-router(5)之 路由的before家族的更多相关文章

  1. vue.js利用vue.router创建前端路由

    node.js方式: 利用node.js安装vue-router模块 cnpm install vue-router 安装完成后我们引入这个模板! 下载vue-router利用script引入方式: ...

  2. Vue Router路由守卫妙用:异步获取数据成功后再进行路由跳转并传递数据,失败则不进行跳转

    问题引入 试想这样一个业务场景: 在用户输入数据,点击提交按钮后,这时发起了ajax请求,如果请求成功, 则跳转到详情页面并展示详情数据,失败则不跳转到详情页面,只是在当前页面给出错误消息. 难点所在 ...

  3. [Vue 牛刀小试]:第十四章 - 编程式导航与实现组件与 Vue Router 之间的解耦

    一.前言 在上一章的学习中,通过举例说明,我们了解了 Vue Router 中命名路由.命名视图的使用方法,以及如何通过 query 查询参数传参,或者是采用 param 传参的方式实现路由间的参数传 ...

  4. Vue躬行记(8)——Vue Router

    虽然Vue.js未提供路由功能,但是官方推出了Vue Router(即vue-router库),以插件的形式支持.它与Vue.js深度集成,可快速的创建单页应用(Single Page Applica ...

  5. Vue.js 2.x笔记:路由Vue Router(6)

    1. Vue Router简介与安装 1.1 Vue Router简介 Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,构建单页面应用. Vue Rout ...

  6. Vue - Router 路由

    路由的注册 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  7. 前端MVC Vue2学习总结(八)——Vue Router路由、Vuex状态管理、Element-UI

    一.Vue Router路由 二.Vuex状态管理 三.Element-UI Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌面端UI框架,手机端有对应框架是 Mint U ...

  8. Vue router 一个路由对应多个视图

    使用命名路由 https://jsfiddle.net/posva/6du90epg/ <script src="https://unpkg.com/vue/dist/vue.js&q ...

  9. [Vue 牛刀小试]:第十二章 - 使用 Vue Router 实现 Vue 中的前端路由控制

    一.前言 前端路由是什么?如果你之前从事的是后端的工作,或者虽然有接触前端,但是并没有使用到单页面应用的话,这个概念对你来说还是会很陌生的.那么,为什么会在单页面应用中存在这么一个概念,以及,前端路由 ...

  10. Vue系列:Vue Router 路由梳理

    Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模块化的.基于组件的路由配置 路由参数. ...

随机推荐

  1. iPhone Safari下iframe不显示滚动条无法滚动的解决方法。iframe的坑!

    <div class="dataTables_wrapper" style="-webkit-overflow-scrolling:touch;overflow:a ...

  2. Linux操作系统之Shell程序设计

    Shell是种命令解释程序,也可以看作是一种命令语言的解释器. 用户在与Shell交互时所输入的命令行必须符合Shell命令的语法和语义规范,才能够被Shell理解并执行.Shell所使用的这种命令语 ...

  3. setup factory 设置自启动

    Registry.SetValue(HKEY_LOCAL_MACHINE, "Software//Microsoft//Windows//CurrentVersion//Run", ...

  4. maven的概念模型及maven坐标

     1.概念模型 项目对象模型:一个maven工程有一个pom.xml文件,通过pom.xml文件定义项目的坐标.项目依赖.项目信息.插件目标等. 依赖管理系统:通过maven的依赖管理对项目所依赖的j ...

  5. jquery实现常用UI布局

    tab html <div class="tab"> <ul class="tab-title"> <li class=" ...

  6. Maven与nexus关系

    一.了解Maven,Maven用来干什么呢 1. 优秀的构建工具 通过简单的命令,能够完成清理.编译.测试.打包.部署等一系列过程.同时,不得不提的是,Maven是跨平台的,无论是在Windows.还 ...

  7. Python MySQL Update

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  8. Python MySQL Order By

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  9. Python MySQL 创建表

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  10. ZOJ - 3870 Team Formation(异或)

    题意:给定N个数,求这N个数中满足A ⊕ B > max{A, B})的AB有多少对.(A,B是N中的某两个数) 分析: 1.异或,首先想到转化为二进制. eg:110011(A)和 1(B)- ...