Vue项目,搜索多次执行相同的push问题
web Vue 2022-11-17 1060 0
关于本站

“最难不过坚持”

本人承接扒站仿站,php网站维护,病毒查杀,网站编辑,网站改版,html制作

有需要网站维护,改版,病毒查杀,网站编辑,网站备案,html制作等相关的工作可以联系我。
本人有多年相关工作经验,也可提供免费咨询,交个朋友。
有需要探讨问题的朋友,也可以加我微信,共同探讨!
微信:15011482830 QQ:408917339

6457203 2613 39
最新评论
https://jueru.net/
评 留言
:weixiao:
评 留言
:shuijiao: :weiqu: :zhenbang: :leng:
评 留言
:yiwen: :yiwen: :yiwen: :yiwen:
评 EasySass: could not generate CSS file. See Output panel for details.
这个业务逻辑多少都有点奇怪了,阅读浏览次数增值在新闻详情页的控制器方法里setInc,这怎么还写进模型事件里了。如果非要用onAfterRead也可以,把新闻文章的内容单独分出来一个news_content表,然后把它和news做关联,然后给news_content表的onAfterRead事件做增值处理,这样点进新闻页内查询到文章内容时才会触发它。
评 TP6模型事件-查询后onAfterRead不好用
文章标签更多
ThinkPHP (254)
Mysql (58)
DedeCms (33)
jQuery (67)
证件照 (1)
setInc (4)
setDec (4)
onclick (5)
打开边栏(ESC) 关闭边栏(ESC)
多次执行相同的push问题,控制台会出现警告

例如:使用this.$router.push({name:‘Search’,params:{keyword:“…”||undefined}})时,如果多次执行相同的push,控制台会出现警告。

let result = this.$router.push({name:"Search",query:{keyword:this.keyword}})
console.log(result)
from clipboard
vue-router.esm.js?3423:2046 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: "/search/213112333?k=213112333".
    at createRouterError (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:1714:15)
    at createNavigationDuplicatedError (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:1702:15)
    at HashHistory.confirmTransition (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:1964:18)
    at HashHistory.transitionTo (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:1900:8)
    at HashHistory.push (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2275:10)
    at eval (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2531:24)
    at new Promise (<anonymous>)
    at VueRouter.push (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2530:12)
    at VueComponent.goSearch (webpack-internal:///./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./src/components/Header/index.vue?vue&type=script&lang=js&:20:20)
    at invokeWithErrorHandling (webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:2892:26)

原因:push是一个promise,promise需要传递成功和失败两个参数,我们的push中没有传递。
方法:this.$router.push({name:‘Search’,params:{keyword:“…”||undefined}},()=>{},()=>{})后面两项分别代表执行成功和失败的回调函数。
这种写法治标不治本,将来在别的组件中push|replace,编程式导航还是会有类似错误
push是VueRouter.prototype的一个方法,在router中的index重写该方法即可(看不懂也没关系,这是前端面试题)

//1、先把VueRouter原型对象的push,保存一份
let originPush = VueRouter.prototype.push;
//2、重写push|replace
//第一个参数:告诉原来的push,跳转的目标位置和传递了哪些参数
VueRouter.prototype.push = function (location,resolve,reject){
    if(resolve && reject){
        originPush.call(this,location,resolve,reject)
    }else{
        originPush.call(this,location,() => {},() => {})
    }
}

另一篇笔记:

编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?

注意:编程式导航(push|replace)才会有这种情况的异常,声明式导航是没有这种问题,因为声明式导航内部已经解决这种问题。
为什么会出现这种现象:
由于vue-router最新版本3.5.2,引入了promise,当传递参数多次且重复,会抛出异常,
第一种解决方案:是给push函数,传入相应的成功的回调与失败的回调,但是以后再用push|replace还是会出现类似现象,因此我们需要从‘根’治病;

this.$router.push({name:'search', params:{keyword:this.keyword}, query:{k:this.keyword.toUpperCase()}}, ()=>{}, ()=>{})
第二种解决方案: 在配置路由时重写push方法

// 把VueRouter 原型对象的push, replace方法, 保存一份
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
// 重写push, replace
// 第一个参数: 告诉原来的push方法, 要往哪里跳(传递那些参数)
// 第二个参数: 成功时的回调
// 第三个参数: 失败时的回调
// call || apply的区别
// 相同点: 都可以调用函数一次, 都可以修改this指向一次
// 不同点: call传递参数用逗号隔开, apply则使用数组
VueRouter.prototype.push = function(location, resolve, reject){
  if(resolve && reject) {
    // originPush()  不能直接调用, 此时this会指向window, 要使用 .call进行调用
    originPush.call(this, location, resolve, reject, );
  }else {
    originPush.call(this, location, ()=>{}, ()=>{})
  }
},
VueRouter.prototype.replace = function(location, resolve, reject){
  if(resolve && reject) {
    originReplace.call(this, location, resolve, reject, );
  }else {
    originReplace.call(this, location, ()=>{}, ()=>{})
  }
}
from clipboard

版权声明:本篇文章来源于网络。 来源链接

相关推荐
接口返回request failed with status code 500错误
Vue | 2023-02-20 2638
接口返回request failed with status code 500错误 登录界面,验证账号密码成功,但是返回500错误 可能原因:linux服务器,用tp5搭建的后台及接口,会生成一部分缓存文件,但是linux默认没有权限创建数据,所以,只要在缓存文件夹runtime添加写的权限即可
vue项目npm run serve默认浏览器打开
Vue | 2022-11-16 1892
1、在vue项目中,输入命令行  npm run serve   浏览器实现自动打开功能,需要添加 在配置文件(package.json)中添加 --open 2、浏览器自动打开后,可能会出现网址0.0.0.0:8080页面错误,见图 3、解决方法: 在--open后面再加上--...
评论:0条
评论加载中...
发表评论