vue 监听路由变化

一.watch监听$route($router的对象)

// 监听,当路由发生变化的时候执行
watch:{$route(to,from){console.log(to.path);}
},
===========================================================
// 监听,当路由发生变化的时候执行
watch: {$route: {handler: function(val, oldVal){console.log(val);},// 深度观察监听deep: true}
},
===========================================================
// 监听,当路由发生变化的时候执行
watch: {'$route':'getPath'
},
methods: {getPath(){console.log(this.$route.path);}
}

二. vue-router 的钩子函数(写在与methods同级的地方或者router中)

beforeRouteEnter (to, from, next) {// 在渲染该组件的对应路由被 confirm 前调用// 不!能!获取组件实例 `this`// 因为当钩子执行前,组件实例还没被创建
},
===========================================================
beforeRouteUpdate (to, from, next) {// 在当前路由改变,但是该组件被复用时调用// 举例来说,对于一个带有动态参数的路径 /foo/:id,在 /foo/1 和 /foo/2 之间跳转的时候,// 由于会渲染同样的 Foo 组件,因此组件实例会被复用。而这个钩子就会在这个情况下被调用。// 可以访问组件实例 `this`
},
===========================================================
beforeRouteLeave (to, from, next) {// 导航离开该组件的对应路由时调用// 可以访问组件实例 `this`
},

完整的导航解析流程

  1. 导航被触发。
  2. 在失活的组件里调用 beforeRouteLeave 守卫。
  3. 调用全局的 beforeEach 守卫。
  4. 在重用的组件里调用 beforeRouteUpdate 守卫 (2.2+)。
  5. 在路由配置里调用 beforeEnter。
  6. 解析异步路由组件。
  7. 在被激活的组件里调用 beforeRouteEnter。
  8. 调用全局的 beforeResolve 守卫 (2.5+)。
  9. 导航被确认。
  10. 调用全局的 afterEach 钩子。
  11. 触发 DOM 更新。
  12. 调用 beforeRouteEnter 守卫中传给 next 的回调函数,创建好的组件实例会作为回调函数的参数传入。

三. 路由守卫

import Vue from "vue";
import VueRouter from "vue-router";
import store from '@/store'
Vue.use(VueRouter);const routes = [***]const router = new VueRouter({routes
});// 设置路由守卫,在进页面之前,判断有token,才进入页面,否则返回登录页面
router.beforeEach((to, from, next) => {// 判断要去的路由有没有 noRequireToken// to.matched.some(r => r.meta.noRequireToken) or to.meta.noRequireTokenif (to.matched.some(r => !r.meta.noRequireToken)) {let token = store.getters.getTokenconsole.log("token:", token,to.fullPath)if (token) {next(); //有token,进行request请求,后台还会验证token} else {next({name: "Login", // 使用params参数不会消失,meta刷新后消失;params必须和query一起用?// path:"/login",// 将刚刚要去的路由path(却无权限)作为参数,方便登录成功后直接跳转到该路由,这要进一步在登陆页面判断query: { redirect: to.fullPath },// params: { redirect: to.fullPath },// meta: { redirect: to.fullPath },});}} else {next(); //如果无需token,那么随它去吧}
});export default router;

组件独享守卫

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

官网介绍 vue-router

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278658.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

音频剪切_音频编辑入门指南:剪切,修剪和排列

音频剪切Audacity novices often start with lofty project ideas, but sometimes they lack the basics. Knowing how to cut and trim tracks is basic audio editing and is a fundamental starting point for making more elaborate arrangements. 大胆的新手通常从崇高的项…

Mybatis自定义SQL拦截器

本博客介绍的是继承Mybatis提供的Interface接口,自定义拦截器,然后将项目中的sql拦截一下,打印到控制台。 先自定义一个拦截器 package com.muses.taoshop.common.core.database.config;import org.apache.commons.lang3.StringUtils; import…

搭建spring boot环境并测试一个controller

Idea搭建spring boot环境一、新建项目二、起步依赖三、编写SpringBoot引导类四、编写Controller五、热部署一、新建项目 1.新建project 2.选择SpringInitializr,选择jdk,没有则需要下载并配置(若选择Maven工程则需要自己添加pom.xml所需依赖坐标和Java…

音频噪声抑制_音频编辑入门指南:基本噪声消除

音频噪声抑制Laying down some vocals? Starting your own podcast? Here’s how to remove noise from a messy audio track in Audacity quickly and easily. 放下人声? 开始自己的播客? 这是在Audacity中快速轻松地消除杂乱音轨中噪声的方法。 Th…

Dubbo集群容错

转自dubbo官网文档http://dubbo.apache.org/zh-cn/blog/dubbo-cluster-error-handling.html Design For failure 在分布式系统中,集群某个某些节点出现问题是大概率事件,因此在设计分布式RPC框架的过程中,必须要把失败作为设计的一等公民来对…

Linux基础(day53)

2019独角兽企业重金招聘Python工程师标准>>> 12.21 php-fpm的pool php-fpm的pool目录概要 vim /usr/local/php/etc/php-fpm.conf//在[global]部分增加include etc/php-fpm.d/*.confmkdir /usr/local/php/etc/php-fpm.d/cd /usr/local/php/etc/php-fpm.d/vim www.co…

Mysql+Navicat for Mysql

一、mysql 1.下载安装 Mysql官网下载地址 下载后解压 .zip (或安装.msi) 2.可加入全局变量mysqld (可选) 我的电脑->属性->高级->环境变量->Path(系统变量),添加mysql下的bin目录,如 D:\Pr…

公钥,私钥和数字签名

一、公钥加密 假设一下,我找了两个数字,一个是1,一个是2。我喜欢2这个数字,就保留起来,不告诉你们(私钥),然后我告诉大家,1是我的公钥。 我有一个文件,不能让别人看&…

MySQL中的日志类型(二)-General query log

简介 General query log记录客户端的连接和断开,以及从客户端发来的每一个SQL语句。 日志内容格式 General query log可以记录在文件中,也可以记录在表中,格式如下:在文件中会记录时间、线程ID、命令类型以及执行的语句示例如下&a…

android wi-fi_如何在Android手机上查找3G或Wi-Fi速度

android wi-fiAre you curious about what kind of connection speed you are getting with your Android phone? Today we’ll take a look at how to easily check your Wi-Fi or 3G speeds with Speedtest.net’s Speed Test app. 您是否对Android手机的连接速度感到好奇&a…

vue引入全局less实现全局变量的控制

vue引入全局less1.设置全局样式变量的好处:2.以less为例(sass等同原理)1.vue-cli2搭建的项目(1)2.vue-cli2搭建的项目(2)3.vue-cli3、vue-cli43.vue-cli2和vue-cli3的区别4.vue-cli3和vue-cli4的…

如何在eclipse中对项目进行重新编译

有时由于eclipse异常关闭,当我们重启Eclipse,在启动项目时,会报错,说:ClassNotFound类似的错误,引起这种问题的原因可能是由于,Eclipse异常关闭引起的。 解决:在一个项目中&#xff…

SQL 查询数据库中包含指定字符串的相关表和相关记录

declare str varchar(100)set str我要找的 --要搜索的字符串declare s varchar(8000)declare tb cursor local forselect if exists(select 1 from [b.name] where [a.name] like %str%)print [b.name].[a.name]from syscolumns a join sysobjects b on a.idb.idwhere b.xtype…

如何在Gmail的图片中插入超链接

Adding hyperlinks is an efficient way of getting your reader to the intended web page. Though it’s no secret that you can add hyperlinks to text, Gmail also lets you add hyperlinks to images in the body of the email. Here’s how to make it happen. 添加超链…

内联元素居中

父元素&#xff1a; height:100px; line-height:100px; // 与高相同 text-align:center; 子元素: display:inline; vertical-align: middle; 适用图片、文字 <div><div class"wrapper"><span>我是文字</span></div><div class&qu…

防止html标签转义

function htmlDecode ( str ) {var ele document.createElement(span);ele.innerHTML str;return ele.textContent;} 例如body下边所有的p标签都防止转义&#xff1a; $.each($("body").find(p),function(){this.innerHTML htmlDecode(this.innerHTML);}); 转载于…

新垣结衣自拍照_如何阻止自拍照出现在iPhone的自拍照专辑中

新垣结衣自拍照Khamosh PathakKhamosh PathakThe Photos app on your iPhone automatically populates all photos from the front-facing camera in the Selfies album. But what if you don’t want a photo to appear there? Here are a couple of solutions. iPhone上的“…

前端个人笔记

前端个人笔记1.vue项目安装依赖/插件时忘记--save&#xff0c;再次install出问题并且没有报错。2.margin移动元素不显示背景色3.新知识&#xff1a;media 条件样式4.入坑&#xff1a;row和col不能分离&#xff0c;span24不能不写5.聚焦实现滚动到指定元素1.vue项目安装依赖/插件…

kernel中对文件的读写【学习笔记】【原创】

/*1. 头文件 */ #include <linux/init.h> #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/kernel.h> #include <linux/list.h> #include <linux/fs.h> #include <linux/uaccess.h>MODULE_PARM_DESC(iva…

ssm项目快速搭建(注解)-依赖

父层jar包版本控制&#xff0c;管理配置 <!-- 集中定义依赖版本号 --> <properties> <junit.version>4.12</junit.version> <spring.version>4.2.4.RELEASE</spring.version> <pagehelper.version>4.0.0<…