gpt4 book ai didi

javascript - 如何用vue.js和vue router实现当前路由子菜单的子菜单

转载 作者:搜寻专家 更新时间:2023-10-30 22:16:36 25 4
gpt4 key购买 nike

我正在尝试在我的 Vue 应用程序上设置导航,让主菜单位于顶部,由所有根级路由填充,如果路由有任何子级,则提供侧边菜单。像这样:

Goal

我目前的设计有主导航,路由链接在顶部,路由 View 在下方。我已经能够使侧面菜单正常工作,因此它仅在我选择 Travelers 并正确更新组件/内容时才会显示。但是,我无法正确路由事物,当我单击子菜单中的链接之一时,它不会附加到当前路径。因此,当我在 localhost/Traveler 中单击 View 并单击 View 时,url 更改为 localhost/View/ 而不是 >本地主机/旅行者/ View 。当我在子菜单中选择某些内容时,顶部菜单上的选择也会取消选择。

Unselecting

而且我无法通过localhost/Traveler/View之类的方式访问页面,仅localhost/View

我开始重新阅读 nested routes 上的文档当我开始写这篇文章时,我想我意识到我应该在每个级别创建一个新路由器,这不是我在下面的代码中所做的事情。

我也不确定如何访问当前路线的 child 。我试过这样显示它们:

  <h2>Route: {{ $route.name }}</h2>
<ul id="example-1">
<li v-for="child in $route.children">
{{ child.name }}
</li>
</ul>

但我一无所获。他们应该作为参数传递吗?还是不是那么容易访问?

任何建议或帮助将不胜感激。

包含带有路由器链接的顶部菜单导航和下方的路由器 View 。

<template>
<div id="app" class="container-fluid">
<div class="row">
<div style="width:100%">
<nav-menu params="route: route"></nav-menu>
</div>

</div>

<div class="row">
<div>
<router-view></router-view>
</div>
</div>
</div>
</template>
<script>
import NavMenu from './nav-menu'

export default {
components: {
'nav-menu': NavMenu
},

data() {
return {}
}
}
</script>

顶部导航菜单

获取路线

<template>
<nav class="site-header sticky-top py-1">
<div class="container d-flex flex-column flex-md-row justify-content-between">
<a class="nav-item" v-for="(route, index) in routes" :key="index">
<router-link :to="{path: route.path, params: { idk: 1 }}" exact-active-class="active">
<icon :icon="route.icon" class="mr-2" /><span>{{ route.display }}</span>
</router-link>
</a>
</div>
</nav>
</template>
<script>
import { routes } from '../router/routes'

export default {
data() {
return {
routes,
collapsed: true
}
},
methods: {
toggleCollapsed: function (event) {
this.collapsed = !this.collapsed
}
}
}
</script>

旅行者页面/ View

当前的 Traveler 页面有一个侧边栏菜单和另一个内容的路由 View :

<template>
<div id="app" class="container-fluid">
<div class="wrapper">
<traveler-menu params="route: route"></traveler-menu>

<div id="content">
<router-view name="travlerview"></router-view>
</div>
</div>
</div>
</template>
<script>
import TravelerMenu from './traveler-menu'
export default {
components: {
'traveler-menu': TravelerMenu
},
data() {
return {}
}
}
</script>

侧边栏/旅行者菜单

<template>
<nav id="sidebar">
<div class="sidebar-header">
<h3>Route's Children:</h3>
</div>

<ul class="list-unstyled components">
<li>
<a class="nav-item" v-for="(route, index) in travelerroutes" :key="index">
<router-link :to="{path: route.path, params: { idk: 1 }}" exact-active-class="active">
<icon :icon="route.icon" class="mr-2" /><span>{{ route.display }}</span>
</router-link>
</a>
</li>
</ul>

</nav>

</template>

<script>
import { travelerroutes } from '../../router/travelerroutes'
export default {
data() {
console.log(travelerroutes);
return {
travelerroutes,
collapsed: true
}
},
methods: {
toggleCollapsed: function (event) {
this.collapsed = !this.collapsed
}
}
}
</script>

路线

import CounterExample from 'components/counter-example'
import FetchData from 'components/fetch-data'
import HomePage from 'components/home-page'
import TestPage from 'components/test-page'
import Travelers from 'components/Traveler/traveler-root'
import { travelerroutes } from './travelerroutes'

export const routes = [
{ name: 'home', path: '/', component: HomePage, display: 'Home', icon: 'home' },
{ name: 'counter', path: '/counter', component: CounterExample, display: 'Counter', icon: 'graduation-cap' },
{ name: 'fetch-data', path: '/fetch-data', component: FetchData, display: 'Fetch data', icon: 'list' },
{ name: 'test-page', path: '/test-page', component: TestPage, display: 'Test Page', icon: 'list' },
{
name: 'traveler-root', path: '/traveler', component: Travelers, display: 'Travelers', icon: 'list', children: travelerroutes
}
]

旅行者路线 (travelerroutes.js)

import TestPage from 'components/test-page'
import ViewTravelers from 'components/Traveler/TravelerPages/view-travelers'

export const travelerroutes = [{
name: 'View',
path: '/View',
display: 'View', icon: 'list',
components: {
travlerview: TestPage
}
},
{
name: 'Create',
path: '/Create',
display: 'Create', icon: 'list',
components: {
travlerview: ViewTravelers
}
},
{
name: 'Edit',
path: '/Edit',
display: 'Edit', icon: 'list',
components: {
travlerview: ViewTravelers
}
}];

路由器/index.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import { routes } from './routes'

Vue.use(VueRouter)

let router = new VueRouter({
mode: 'history',
routes
})

export default router

应用程序.js

import Vue from 'vue'
import axios from 'axios'
import router from './router/index'
import store from './store'
import { sync } from 'vuex-router-sync'
import App from 'components/app-root'
import { FontAwesomeIcon } from './icons'

// Registration of global components
Vue.component('icon', FontAwesomeIcon)

Vue.prototype.$http = axios

sync(store, router)

const app = new Vue({
store,
router,
...App
})

export {
app,
router,
store
}

如果您需要更多详细信息、上下文或代码,请告诉我。

最佳答案

您不需要创建新的路由器实例,而是观察 $route 属性并在它发生变化时创建侧边栏导航菜单。您需要从 $router.options.routes 中提取子路由。这是一个例子:

const router = new VueRouter({
routes: [{
name: 'home',
path: '/',
component: {
template: '<div>Home</div>'
}
},
{
name: 'foo',
path: '/foo',
component: {
template: '<div>Foo<router-view></router-view></div>'
},
children: [{
name: 'foo.baz',
path: '/baz',
component: {
template: '<div>Baz</div>'
}
}, {
name: 'foo.tar',
path: '/tar',
component: {
template: '<div>Tar</div>'
}
}]
},
{
name: 'bar',
path: '/bar',
component: {
template: '<div>Bar<router-view></router-view></div>'
},
children: [{
name: 'bar.zim',
path: '/zim',
component: {
template: '<div>Zim</div>'
}
}, {
name: 'bar.zug',
path: '/zug',
component: {
template: '<div>Zug</div>'
}
}]
}
]
})

new Vue({
el: '#app',
router,
data() {
return {
children: []
}
},
watch: {
$route: function(current) {
const route = this.$router.options.routes.find(route => route.path === current.path)

if (route && Array.isArray(route.children)) {
this.children = route.children
} else if (route) {
this.children = []
}
}
}
})
* {
margin: 0;
padding: 0;
}

html,
body,
#app {
width: 100%;
height: 100%;
}

#top {
border: 1px solid black;
}

#top ul {
display: flex;
flex-direction: row;
justify-content: flex-start;
list-style-type: none;
}

li {
padding: 1rem;
text-align: center;
text-transform: uppercase;
}

li a {
text-decoration: none;
font-weight: bold;
color: black;
}

#sidebar {
height: 50%;
width: 100px;
border: 1px solid black;
}

#content {
width: 50%;
}

#content,
#content>div {
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://npmcdn.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<div id="top">
<ul>
<li v-for="item in $router.options.routes" :key="item.path" :style="{ 'background-color': $route.name.includes(item.name) ? 'rgba(197,225,165,1)' : 'white' }">
<router-link :to="item.path">{{ item.name }}</router-link>
</li>
</ul>
</div>
<div style="display: flex; flex-direction: row; height: 100%; width: 100%;">
<div id="sidebar">
<ul>
<li v-for="item in children" :key="item.path" :style="{ 'background-color': $route.name === item.name ? 'rgba(197,225,165,1)' : 'white' }">
<router-link :to="item.path">{{ item.name.split('.').pop() }}</router-link>
</li>
</ul>
</div>
<div id="content">
<router-view></router-view>
</div>
</div>
</div>

关于javascript - 如何用vue.js和vue router实现当前路由子菜单的子菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54373245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com