Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

重复点击路由,导致提示避免到当前位置的冗余导航该怎么解决

import Vue from 'vue'

import VueRouter from 'vue-router'

import Home from '../views/Home/Home.vue'

import Recommend from '../views/Recommend/Recommend.vue'

import Me from '../views/Me/Me.vue'

import Chat from '../views/Chat/Chat.vue'

import Search from '../views/Search/Search.vue'

//  这里按照网上的方法加入了以下代码,但是还是不管用

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location)?{

 return originalPush.call(this, location).catch(err => err)

}

Vue.use(VueRouter)

const routes =?[

 {

 path: '/',

 redirect: '/home'

 },

 {

 path: '/home',

 component: Home,

 children: [

 { path: '/recommend', component: Recommend },

 { path: '/search', component: Search },

 { path: '/me', component: Me },

 { path: '/chat', component: Chat }

 ]

 }

]

const router = new VueRouter({

 routes

})

export default router

这是我的代码,我在按照网上提示,加入了
`const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location)?{

return originalPush.call(this, location).catch(err => err)

}`
但是还是报一样的错误,是我加点位置有问题吗?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

这段代码没问题,你先确定一下是不是报的这个错误NavigationDuplicated
image.png

解决这个报错的关键是this.$router.push(...).catch(err => err)要有后面的catch。因为跳转方法返回了一个promise对象,要有处理拒绝的方法。

你添加的这段代码,相当于覆写了一下push方法,统一添加了处理拒绝。调用push应该没问题。

然后你在检查一下,路由跳转的时候是不是调用的push方法,是不是用的replace
如果是就也覆写一下

const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace (location) {
    return originalReplace.call(this, location).catch(err => err)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...