vue中点击重复路径报错发问题

本文介绍了如何在Vue项目中处理导航栏子菜单重复点击导致的错误。通过修改Router原型上的push方法,捕获并处理可能抛出的错误。同时,展示了如何在页面组件中监听路由变化,实现在路由变化时重新获取数据,确保页面内容的正确更新。此方法适用于需要动态加载数据且依赖路由参数的场景。
摘要由CSDN通过智能技术生成

在项目中遇到导航栏子菜单重复路劲报错时,使用监听解决
一,在router下index.js中加上这一句话

///解决重复点菜单报红问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

二,如何在点击页面写

/* eslint-disable no-unused-vars */
<template>
 <div :key="index" v-for="(items,index) in listcase" >
   <p  @click="caseCenter(items.code)">{{items.name}}</p>
 </div>
</template>
<script>
import Cards from './cards.vue'
import Carousel from './carousel.vue'
export default {
  name: 'homePage',
  data () {
    return {
      listcase:[]
    }
  },
  methods: {
   init () {
      this.$http.get('自己的后台路径').then(res => {
        if (res.data.data != undefined && res.data.data != null) {
          this.listcase= res.data.data
        }
      })
    },
     caseCenterr (code) {
      var self = this
      self.$router.push({
        name: 'caseCenterType',
        query: {//这里是query,不是params
          code: code
        }
      })
    },
  }
}

</script>
<style scoped>
</style>

三,在需要跳转的页面上写

<template>
<div>
   <div :key="index" v-for="(data,index) in paginatedCards" >
         <div>{{data.name}}</div>
                 
              </div>
          
        </div>

      </b-container>
    </div>
   <Footer/>
</div>
</template>

<script >
export default {
  name: 'type',
  data () {
    return {
      cards: [],
    }
  },
  //这个是重点
  created () {
    this.init()
  },
  //这个是重点
  watch:{
    '$route' () {
      this.init()
    }
  },
  methods: {
    Info (code) {
      var self = this
      self.$router.push({
        name: 'dfdf',
        query: {
          code: code
        }
      })
    },
    init () {
    //这个是重点
      this.code = this.$route.query.code
      this.$http.get('api/jks/sdsjk/sdsd/' + this.code).then(res => {
        if (res != undefined) {
          this.cards = res.data.data
        }
      })
    }
  }
}
</script>
<style >
</style>

可以了,大功告成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值