一样的和前面路由钩子类似的步骤

首先在demo下面的components下面新建一个test.vue组件

test组件代码

<template>
<div class="test_box">
<p @click="go">测试组件内部守卫的作用,点击跳到HelloWorld</p>
</div>
</template>
<script>
export default {
data() {
return { }
},
methods: {
go() {
this.$router.push({ name: 'HelloWorld' })
}
},
beforeRouteEnter(to, from, next) {
console.log(this, 'beforeRouteEnter'); // undefined
console.log(to, '组件独享守卫beforeRouteEnter第一个参数');
console.log(from, '组件独享守卫beforeRouteEnter第二个参数');
console.log(next, '组件独享守卫beforeRouteEnter第三个参数');
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, '组件独享守卫beforeRouteUpdate第一个参数');
console.log(from, '组件独享守beforeRouteUpdate卫第二个参数');
console.log(next, '组件独享守beforeRouteUpdate卫第三个参数');
next();
},
beforeRouteLeave(to, from, next) {
// 导航离开该组件的对应路由时调用
// 可以访问组件实例 `this`
console.log(this, 'beforeRouteLeave'); //当前组件实例
console.log(to, '组件独享守卫beforeRouteLeave第一个参数');
console.log(from, '组件独享守卫beforeRouteLeave第二个参数');
console.log(next, '组件独享守卫beforeRouteLeave第三个参数');
next();
}
} </script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped> </style>

helloWord组件代码

<template>
<div class="sider_box">
<p @click="go">第一个页面</p>
<p @click="goTest">触发让它跳到test页面检查组件守卫功能</p>
</div>
</template>
<script>
export default {
data() {
return { }
},
methods: {
go(){
this.$router.push({name:'navMenu'})
},
goTest(){
this.$router.push({name:'test'})
}
}
} </script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped> </style>

先从helloword跳到test可以看到控制台打印

再从test跳到helloword可以看到打印如下:

vue组件独享守卫钩子函数参数详解(beforeRouteEnter、beforeRouteUpdate、beforeRouteLeave)的更多相关文章

  1. vue路由导航守卫及前置后置钩子函数参数详解

    首先构建一个测试demo如下图: 接着来探讨路由配置界面 import Vue from 'vue' import Router from 'vue-router' // import HelloWo ...

  2. PHP date函数参数详解

    PHP date函数参数详解 作者: 字体:[增加 减小] 类型:转载       time()在PHP中是得到一个数字,这个数字表示从1970-01-01到现在共走了多少秒,很奇怪吧 不过这样方便计 ...

  3. Python函数参数详解

    Python函数参数详解 形参与实参 什么是形参 在定义函数阶段定义的参数称之为形式参数,简称形参,相当于变量名. 什么是实参 在调用函数阶段传入的值称为实际参数,简称实参.相当于"变量值& ...

  4. Go语言Slice作为函数参数详解

    Go语言Slice作为函数参数详解 前言 首先要明确Go语言中实质只有值传递,引用传递和指针传递是相对于参数类型来说. 个人认为上诉的结论不对,把引用类型看做对指针的封装,一般封装为结构体,结构体是值 ...

  5. vue的生命函数周期以及钩子函数的详解

      首先我们先附上官网的图 图中展现出的是vue整个生命周期以及钩子函数 1- beforeCreate(创建前) 2- created(创建完成) 3- beforeMount(挂载前) 4- mo ...

  6. 【集成学习】sklearn中xgboot模块中fit函数参数详解(fit model for train data)

    参数解释,后续补上. # -*- coding: utf-8 -*- """ ############################################## ...

  7. vue路由独享守卫beforeEnter

    在某个路由中,使用beforeEnter()方法,参数是to,from,next 和全局路由守卫的用法是一样的 例子: import Vue from 'vue' import Router from ...

  8. CGBitmapContextCreate函数参数详解

    函数原型: CGContextRef CGBitmapContextCreate ( void *data, size_t width, size_t height, size_t bitsPerCo ...

  9. CGBitmapContextCreate函数参数详解 以及在 ios7下变化

    函数原型: CGContextRef CGBitmapContextCreate ( void *data,    size_t width,    size_t height,    size_t ...

随机推荐

  1. yii第二步

    yii第二步: main.php 'urlManager'=>array('urlFormat'=>'path','rules'=>array('game/guess/<g:\ ...

  2. EFS 你应该知道的事

    需要备份或者还保留这个路径 %USERPROFILE%\AppData\Roaming\Microsoft\Crypto\RSA certmgr.msc 个人证书导出你开始使用EFS加密后的证书 ci ...

  3. LeetCode--067--二进制求和

    问题描述: 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1&quo ...

  4. 3 python 基本数据类型

    1.python的基本数据类型 1.字符串 不可变数据类型 2.int //整除 %取余 bit_length() print(a.bit_length()) #打印某个数字类型的二进制长度 3.bo ...

  5. SQL Server 创建定时任务JOB

    1.    SQL Server 代理  → 作业(右键)→新建作业 2.在[常规]中输入 “名称” 并确认已勾选 “已启动”. 3.在[步骤]中 "新建步骤": 填写 “步骤名称 ...

  6. ScoketTimeout Exception浅析

    以前都是用WebService的方式调用服务方的服务,此次直接调用别人的http服务. 使用的客户端是org.apache.http.client.HttpClient. 用的httpclient-4 ...

  7. iframe刷新父页面

    iframe页面是内嵌到父页面的,当点击iframe页面的服务器控件时,默认只刷新iframe页面,父页面是不会刷新的.若想刷新父页面,可以使用js来实现,如 1. parent.location.r ...

  8. 87. Scramble String *HARD* 动态规划

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  9. 55. 45. Jump Game II *HARD*

    1. Given an array of non-negative integers, you are initially positioned at the first index of the a ...

  10. 超详细:Python(wordcloud+jieba)生成中文词云图

    # coding: utf-8 import jieba from scipy.misc import imread # 这是一个处理图像的函数 from wordcloud import WordC ...