如何使用路由器将 v-list-item 链接到组件?

Jrm*_*Del 6 vue.js vue-router vuetify.js

我正在使用 Vuetify,但似乎无法解决此问题。我有一个<v-list>在我<v-navigation-drawer>看起来像这样:

SideDash.vue 组件

<template>
    <v-navigation-drawer
        v-model="drawer" clipped
        :mini-variant.sync='mini'
        width=240 absolute permanent
    >
        <v-list
            flat dense nav class="py-1"
        >
            <v-list-item-group color='primary' mandatory>
                <v-list-item
                    v-for="item in items"
                    :key="item.title"
                    dense
                    router :to="item.route"
                >
                    <v-list-item-icon>
                        <v-icon>{{ item.icon }}</v-icon>
                    </v-list-item-icon>
                    <v-list-item-content>
                        <v-list-title>{{ item.title }}</v-list-title>
                    </v-list-item-content>

                </v-list-item>
            </v-list-item-group>
        </v-list>
    </v-navigation-drawer>
</template>


<script>
export default {
    data() {
        return {
            drawer: true,
            items: [
                {icon: 'mdi-speedometer', title:'Dashboard', route:'/dashboard'},
                {icon: 'mdi-tools', title:'Tools', route:'/tools'}
            ],
        }
        
    }
    
}
</script>
Run Code Online (Sandbox Code Playgroud)

路由器/index.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import Dashboard from '@/views/Dashboard.vue'
import Tools from '@/views/Tools.vue'


Vue.use(VueRouter)

const routes = [
  {
    path: '/dashboard',
    name: 'dashboard',
    component: Dashboard
  },
  {
    path: '/tools',
    name: 'tools',
    component: Tools
  }
]

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

export default router
Run Code Online (Sandbox Code Playgroud)

应用程序

<template>
  <v-app>
    <nav>
      <Navbar/>
    </nav>
    
    <v-content>
      <SideDash/>
    </v-content>
    
  </v-app>
</template>

<script>
import Navbar from './components/Navbar';
import SideDash from './components/SideDash'

export default {
  name: 'App',

  components: {
    Navbar,
    SideDash
  },

  data: () => ({

  }),
};
</script>
Run Code Online (Sandbox Code Playgroud)

这相当简单,在我将道具添加:to="item.route"到我的v-list-item. 这使我的清单完全消失了。我最终得到了一个没有任何内容的导航抽屉,我不知道为什么。我在这里错过了什么?谢谢你的帮助

PS:这可能会有所帮助,但是当我<router-view></router-view><v-content></v-content>App.vue 中添加时,我得到一个空白页面,甚至没有我的导航栏或导航抽屉。我觉得它可能来自我的路由器设置。我检查了我的 package.json 并得到了它

"vue-router": "^3.1.3"
Run Code Online (Sandbox Code Playgroud)

编辑

好吧,我发现了错误……这很愚蠢。在文件main.jsimport router from './router'被注释为例如routerinside new Vue(...)。我保留了此处显示的代码并且它有效。

Hal*_*lex 6

关于为什么该:to方法有效但@click方法无效的任何建议?

作品

<v-list-item 
   :to="item.route"
   v-else
   :key="i"
   link
>
Run Code Online (Sandbox Code Playgroud)

没有

  <v-list-item 
    @click="$router({path: item.route})"
    v-else
    :key="i"
    link
  >
Run Code Online (Sandbox Code Playgroud)

  • `$router({path: item.route})` 对我不起作用。我不得不将其更改为 `$router.push({path: item.route})` (3认同)

acb*_*ter 5

最后,我成功了。

<v-list-item :key="i" link @click="$router.push({ path: item.route })">
  <v-list-item-action>
    <v-icon>{{ item.icon }}</v-icon>
  </v-list-item-action>
  <v-list-item-content>
    <v-list-item-title class="grey--text">
      {{ item.text }}
    </v-list-item-title>
  </v-list-item-content>
</v-list-item>
Run Code Online (Sandbox Code Playgroud)

来源: https: //router.vuejs.org/guide/essentials/navigation.html

他们说:“点击<router-link :to="...">就相当于打电话router.push(...)。”


编辑:@click="$router.push({ path: item.route }).catch(err => {})如果你不想看就使用"Error: Avoided redundant navigation to current location: "/about".

来源:NavigationDuplicated 不允许导航到当前位置(“/search”)[vuejs]


cha*_*ans 2

上面的代码看起来是正确的,需要在 v-list item click 事件 @click="$router({path: item.route})"中给出一个小修复

items: [
                {icon: 'mdi-speedometer', title:'Dashboard', route:'/dashboard'},
                {icon: 'mdi-tools', title:'Tools', route:'/tools'}
            ],


<template>
    <v-navigation-drawer
        v-model="drawer" clipped
        :mini-variant.sync='mini'
        width=240 absolute permanent
    >
        <v-list
            flat dense nav class="py-1"
        >
            <v-list-item-group color='primary' mandatory>
                <v-list-item
                    v-for="item in items"
                    :key="item.title"
                    dense
                    @click="$router({path: item.route})"
                >
                    <v-list-item-icon>
                        <v-icon>{{ item.icon }}</v-icon>
                    </v-list-item-icon>
                    <v-list-item-content>
                        <v-list-title>{{ item.title }}</v-list-title>
                    </v-list-item-content>

                </v-list-item>
            </v-list-item-group>
        </v-list>
    </v-navigation-drawer>
</template>
Run Code Online (Sandbox Code Playgroud)

  • `@click="$router({path: item.route})"` 和 `:to="item.route"` 有什么区别 (2认同)