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重复路由刷新)的更多相关文章

  1. 01-路由跳转 安装less this.$router.replace(path) 解决vue/cli3.0语法报错问题

    2==解决vue2.0里面控制台包的一些语法错误. https://www.jianshu.com/p/5e0a1541418b 在build==>webpack.base.conf.j下注释掉 ...

  2. 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 ...

  3. git push报错大文件,删除后重新commit依然报错

    git push报错: github不能上传大文件,按道理删掉重新提交就行了 可是删掉后,git add -A,再git commit,再git push,依然报错 后来我想明白了 github上传时 ...

  4. 解决上传代码到GitHub报错Push rejected: Push to origin/master was rejected

    最近在 push 代码到 github 时,IDEA报错 Push rejected: Push to origin/master was rejected 在网友找了一圈,发现都不是想要的答案 于是 ...

  5. vue init webpack nameXXX 报错问题:

    vue新建demo项目报错如下: M:\lhhVueTest>vue init webpack L21_VueProject vue-cli · Failed to download repo ...

  6. vue 表单校验报错 "Error: please transfer a valid prop path to form item!"

    vue 表单校验报错 "Error: please transfer a valid prop path to form item!" 原因:prop的内容和rules中定义的名称 ...

  7. vue中使用JSX报错,如何解决

    Support for the experimental syntax 'jsx' isn't currently enabled (32:12): 30 | }, 31 | render() { & ...

  8. vue Blob 下载附件报错

    vue Blob 下载附件报错,不妨试试: window.location.href=后台地址

  9. 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 ...

随机推荐

  1. Android Jetpack基本架构之ViewModel+LiveData+DataBinding入门

    前提:导入所有依赖,开启DataBinding app的build.gradle android { defaultConfig { ... dataBinding { enabled true } ...

  2. python 动图gif合成与分解

    合成 #!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import sys import imageio def main(imgs_ ...

  3. vue-class和style样式绑定

    前言 操作元素的 class 样式列表和 style 内联样式为数据绑定是前端开发中一个常见的需求,这些样式都属于元素的属性 attribute ,因此我们可以通过 v-bind 来动态绑定元素的样式 ...

  4. <题解>「LibreOJ NOIP Round #1」序列划分

    solutions 题面loj#542 对我来说,这或许已经超出了我的能力,我,只能看题解 不知道我写完这一篇题解之后,会不会对我的构造题有一点点的帮助 让我在这类题的解决上能过有一些提升 直接说明白 ...

  5. layui的CRUD案列

    用layui来实现一个简单的二级权限和增删改查案列 利用layui提供的组件(table , layer , form,tree)来进行开发 写一个简单的登录界面   根据用户的ID来 获取用户所对应 ...

  6. Linux原始套接字抓取底层报文

    1.原始套接字使用场景 我们平常所用到的网络编程都是在应用层收发数据,每个程序只能收到发给自己的数据,即每个程序只能收到来自该程序绑定的端口的数据.收到的数据往往只包括应用层数据,原有的头部信息在传递 ...

  7. JDK7&JDK9处理异常新特性

    1.JDK7新特性是在 try (定义对象,作用域就是try方法体) 复制一个文件实例: 复制文件的原理: 先从硬盘写出到内存中,创建文件输入流对象 FileInputStream fis; 中间是在 ...

  8. Spring Boot 入门系列(二十八) JPA 的实体映射关系,一对一,一对多,多对多关系映射!

    前面讲了Spring Boot 使用 JPA,实现JPA 的增.删.改.查的功能,同时也介绍了JPA的一些查询,自定义SQL查询等使用.JPA使用非常简单,功能非常强大的ORM框架,无需任何数据访问层 ...

  9. SpringAOP-动态代理,日志注入

    SpringAOP 前言: 1.AOP定义? 用来干啥的? 怎么用?(怎么跑通它的思路) 代理模式 为啥要学代理模式? -- 因为是SpringAop的底层 原有的代码不敢动,一动就是Bug,.所以使 ...

  10. python语言介绍及安装

    Python语言简介 Python是什么语言 Python是一种解释型的.可移植的.开源的脚本. 什么是计算机编程 计算机程序:为了让计算机执行某些操作或解决某个问题而编写的一系列有序指令的集合 如何 ...