在项目中遇到导航栏子菜单重复路劲报错时,使用监听解决
一,在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>
可以了,大功告成