代码:
<!-- 侧边栏 -->
<el-col :span="12" :style="{ 'width': '200px' }">
<el-menu default-active="first" class="el-menu-vertical-demo" @select="handleMenuSelect">
<el-menu-item index="first">
<i class="el-icon-menu"></i>
<span slot="title">首页</span>
</el-menu-item>
<el-menu-item index="person">
<i class="el-icon-menu"></i>
<span slot="title">个人中心</span>
</el-menu-item>
<el-menu-item index="score">
<i class="el-icon-document"></i>
<span slot="title">个人成绩</span>
</el-menu-item>
<el-menu-item index="personal">
<i class="el-icon-document"></i>
<span slot="title">成绩管理</span>
</el-menu-item>
<el-menu-item index="manage">
<i class="el-icon-setting"></i>
<span slot="title">人员管理</span>
</el-menu-item>
</el-menu>
</el-col>
<script>
export default {
methods: {
handleMenuSelect(index) {
this.$router.push({ path: '/' + index });
}
}
};
</script>
路由:
const routes = [
{
path: '/home',//路由地址
name: 'home',
component: home,//相对应的组件
redirect: { name: "first" },
children: [
{
path: '/first',
name: 'first',
component: first
},
{
path: '/person',
name: 'person',
component: person
},
{
path: '/personal',
name: 'personal',
component: personal
},
{
path: '/score',
name: 'score',
component: score
},
{
path: '/manage',
name: 'manage',
component: manage
}
]
}
]
目录
解决方法:判断目标路径是否与当前路径相同文章来源:https://www.toymoban.com/news/detail-826392.html
通过 this.$route.path
获取到当前路由的路径文章来源地址https://www.toymoban.com/news/detail-826392.html
handleMenuSelect(index) {
const targetPath = '/' + index;
// 判断目标路径是否与当前路径相同
//通过 this.$route.path 获取到当前路由的路径
if (this.$route.path === targetPath) {
// 如果相同则不进行导航
return;
}
// 否则进行导航
this.$router.push({ path: targetPath });
}
到了这里,关于重复导航到当前位置引起的。Vue Router 提供了一种机制,阻止重复导航到相同的路由路径。的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!