Vue Router 常见问题(push报错、push重复路由刷新)
Vue Router 常见问题
用于记录工作遇到的Vue Router bug及常用方案
router.push报错,Avoided redundant navigation to current location: “/xxx”

大意为 路由频繁点击导致路由重复,该报错对路由跳转功能没有任何影响
解决方案:重写push方法
将异常捕获就不会报错了
let routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
return routerPush.call(this, location).catch(err => err)
}
重复点击路由 刷新页面效果
在重写VueRouter.prototype.push方法时,利用VueRouter的currentRoute对象中的fullpath属性与push函数的参数对比,判断是否为同一路由。如果是,使用一下方法完成页面刷新
方法一:window.location.reload()
问题在于会刷新整个页面,不是很推荐
let routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
let history = this.currentRoute && this.currentRoute.fullPath;
if (location === history) {
window.location.reload(); //整个页面都刷新
}
return routerPush.call(this, location).catch(err => err)
}
方法二:v-if 控制router-view实现局部刷新
较为复杂,但是能够实现局部刷新
由于VueRouter.prototype.push中的this指向VueRouter实例
该实例中存在 "全局Vue实例"(main.js new Vue()生成的) 的引用属性 "app"(this.app)
于是,push函数里面就可以操作Vue实例了。
实现思维:
vue实例使用provide注入一个 getRouterAlive 方法,返回当前vue实例中定义的状态变量 isRouterAlive
再定义一个 reloadChild 方法,修改状态变量 isRouterAlive的值为false,dom更新后再改为true
需要局部刷新的地方 inject 接受 getRouterAlive 这个方法,再在router-view 上绑定
v-if="Object.prototype.toString.call(getRouterAlive) == '[object Function]' && getRouterAlive()"
实现代码:
main.js
// main.js
new Vue({
router,
store,
render: h => h(App),
provide() {
return {
getRouterAlive: this.getRouterAlive,
}
},
data: {
isRouterAlive: true
},
methods: {
getRouterAlive() {
return this.isRouterAlive;
},
reloadChild() {
this.isRouterAlive = false;
this.$nextTick(() => {
this.isRouterAlive = true;
})
}
}
}).$mount('#app')
router.js
// router/index.js
let routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
let history = this.currentRoute && this.currentRoute.fullPath;
if (location === history) {
this.app.reloadChild && this.app.reloadChild();
}
return routerPush.call(this, location).catch(err => err)
}
局部刷新
// 局部刷新
...
<router-view v-if="Object.prototype.toString.call(getRouterAlive) == '[object Function]' && getRouterAlive()"></router-view>
...
inject: ["getRouterAlive"],
...
Vue Router 常见问题(push报错、push重复路由刷新)的更多相关文章
- 01-路由跳转 安装less this.$router.replace(path) 解决vue/cli3.0语法报错问题
2==解决vue2.0里面控制台包的一些语法错误. https://www.jianshu.com/p/5e0a1541418b 在build==>webpack.base.conf.j下注释掉 ...
- git push报错error: failed to push some refs to 'git@github.com'
git push报错error: failed to push some refs to 'git@github.com' $ git push -u origin master To git@git ...
- git push报错大文件,删除后重新commit依然报错
git push报错: github不能上传大文件,按道理删掉重新提交就行了 可是删掉后,git add -A,再git commit,再git push,依然报错 后来我想明白了 github上传时 ...
- 解决上传代码到GitHub报错Push rejected: Push to origin/master was rejected
最近在 push 代码到 github 时,IDEA报错 Push rejected: Push to origin/master was rejected 在网友找了一圈,发现都不是想要的答案 于是 ...
- vue init webpack nameXXX 报错问题:
vue新建demo项目报错如下: M:\lhhVueTest>vue init webpack L21_VueProject vue-cli · Failed to download repo ...
- vue 表单校验报错 "Error: please transfer a valid prop path to form item!"
vue 表单校验报错 "Error: please transfer a valid prop path to form item!" 原因:prop的内容和rules中定义的名称 ...
- vue中使用JSX报错,如何解决
Support for the experimental syntax 'jsx' isn't currently enabled (32:12): 30 | }, 31 | render() { & ...
- vue Blob 下载附件报错
vue Blob 下载附件报错,不妨试试: window.location.href=后台地址
- git push 报错 "Peer certificate cannot be authenticated with known CA certificates"
使用git push -u origin master 命令向远程仓库提交代码时报错:Peer certificate cannot be authenticated with known CA ce ...
随机推荐
- centos7 Tomcat 停止服务时报错: java.net.ConnectException: 拒绝连接 (Connection refused)
2021-08-02 1.问题描述 配置完 server.xml 文件后,执行 shutdown.sh 脚本停止服务,出现以下错误 2. 解决方法 查看当前正在运行的 Java 进程号 # 找到 Ja ...
- Python中的文件处理和数据存储json
前言:每当需要分析或修改存储在文件中的信息时,读取文件都很有用,对数据分析应用程序来说尤其如此. 例如,你可以编写一个这样的程序:读取一个文本文件的内容,重新设置这些数据的格式并将其写入文件,让浏览器 ...
- uniapp H5 打包并部署到 nginx
个人也是了百度了挺久的了,花费的时间( 俩个半小时 ) uniapp 的打包首先要先配置,配置好了才能去进行打包,如图所示. 这只是第一步. 注意: 1.运行基础路径最好用 ./ ,如果配置了其他请自 ...
- 软件测试2021:第一次作业——热身练习(Bug)
案例一: 问题说明:在大学生服务外包创新创业大赛的注册页面填写密码的时候只有偶数位的密码可以通过验证,而基数位的密码不可以 原因分析:在密码验证的时候多加了一条验证,使得基数位的密码不能都通过验证 案 ...
- 即时通讯网-TCPIP族关系图
- 痞子衡嵌入式:原来i.MXRT1xxx系列里也暗藏了Product ID寄存器
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT1xxx系列里暗藏的Product ID寄存器. MCU 厂商在定义一个产品系列时,通常是会预先规划产品发展路线的(即会有一 ...
- Mybatis公司开发常用!
Mybatis核心 本文重点:注解开发,mybatis多表操作,动态SQL(WHERE,SET,IF,SQL-ID减少复用) 代码地址--https://gitee.com/zhangjzm/my-b ...
- PHP中的输出:echo、print、printf、sprintf、print_r和var_dump
大家在面试中,经常会被问到的问题: 请简要说明PHP的打印方式都有哪些? 或者直接点问: 请说明echo.print.print_r的区别 看着很简单,一般会出现在初中级的笔试题中.但是要真正说明白这 ...
- 哪5种IO模型?什么是select/poll/epoll?同步异步阻塞非阻塞有啥区别?全在这讲明白了!
系统中有哪5种IO模型?什么是 select/poll/epoll?同步异步阻塞非阻塞有啥区别? 本文地址http://yangjianyong.cn/?p=84转载无需经过作者本人授权 先解开第一个 ...
- JavaScript对象的两类属性(数据属性与访问器属性)
对JavaScript来说,属性并非只是简单的名称和值,JavaScript用一组特征(attribute)来描述属性 (property). 第一类属性数据属性具有四个特征. value:就是属性的 ...