【发布时间】:2020-11-22 16:27:55
【问题描述】:
我正在尝试实现一个从 Vue Router 原生 $router 对象生成其内容的侧边栏。
因此它遍历路由数组中存在的所有路由。 但是,我想排除那些我不想在我的菜单中显示的内容。
我尝试了不同的方法,但没有按预期工作。请看下面我现在拥有的:
<template>
<div class="three cols card card-content">
<aside class="sidebar">
<h6 class="sidebar-label">Navigate</h6>
<ul class="sidebar-list">
<li><router-link to="/"><i class="ico ri-home-4-line"></i>Home</router-link></li>
<li><router-link to="/"><i class="ico ri-swap-box-line"></i>Changelog <i class="medal bg-salmon text-white"> v.1.0 Beta</i></router-link></li>
<li><router-link to="/"><i class="ico ri-question-line"></i>Getting Started</router-link></li>
</ul>
<h6 class="sidebar-label">Documentation</h6>
<ul class="sidebar-list">
<li v-for="route in routes" :key="route"><router-link :to="route.path">{{route.name}}</router-link></li>
</ul>
</aside>
</div>
</template>
<script>
export default {
created() {
//this iterates trough all the routes in order to implement the sidebar menu
const router = this.$router.getRoutes()
router.forEach(route => this.routes.push({
name: route.name,
path: route.path
}));
//this will remove any specific unwanted routes from the menu
this.routes = this.routes.splice((route) => route.name ==! "Home" && route.name ==! "Table of Contents")
},
data(){
return{
routes: []
}
},
}
</script>
任何帮助将不胜感激,
问候, T.
【问题讨论】:
标签: javascript vue.js vue-router vuejs3