英文:
How can I make the child menus of v-navigation-drawer component on at the first project run?
问题
我正在使用Vue 3和Vuetify 3制作v-navigation-drawer
组件。
v-navigation-drawer
的功能正常,但我希望子菜单在项目首次运行时默认显示,而不需要用户点击。我正在使用v-slot:activator
来显示子菜单。当项目运行时,用户应该看到带有子菜单项的v-navigation-drawer
。
这是我的代码-
<template>
<v-navigation-drawer v-model="drawer" width="325">
<v-list :lines="false" density="compact" nav >
<v-list-group v-for="(item, i) in items" :key="i" :value="item">
<template v-slot:activator="{ props }">
<v-list-item v-bind="props" :prepend-icon="item.icon" :title="item.text" active-color="orange-darken-1"
rounded="xl" ></v-list-item>
</template>
<v-list-item-title v-for="itemdetail in item.subItem" :key="itemdetail.id" :value="itemdetail" >
<template v-if="itemdetail">
<v-list-item :prepend-icon="itemdetail.icon" :title="itemdetail.text"
active-color="teal-darken-1" rounded="xl">
</v-list-item>
</template>
</v-list-item-title>
</v-list-group>
</v-list>
</v-navigation-drawer>
</template>
<script>
export default {
data: () => ({
drawer: true,
items: [{
text: 'Parent Menu',
icon: 'mdi-pier-crane',
subItem: [{
text: 'Child Menu 1',
icon: 'mdi-engine'
},
{
text: 'Child Menu 2',
icon: 'mdi-calculator-variant'
},
{
text: 'Child Menu 3',
icon: 'mdi-list-status'
},
{
text: 'Child Menu 4',
icon: 'mdi-calendar-edit'
},
]
}, ],
}),
}
</script>
英文:
I am making a v-navigation-drawer
component using Vue 3 and Vuetify 3.
The v-navigation-drawer
works well but I want the child menus to show by default without the user's click and when the project first runs. I am using v-slot:activator
for the child menus display. When the project runs, the user should see the v-navigation-drawer
with child menu items.
Here is my code-
<template>
<v-navigation-drawer v-model="drawer" width="325">
<v-list :lines="false" density="compact" nav >
<v-list-group v-for="(item, i) in items" :key="i" :value="item" >
<template v-slot:activator="{ props }">
<v-list-item v-bind="props" :prepend-icon="item.icon" :title="item.text" active-color="orange-darken-1"
rounded="xl" ></v-list-item>
</template>
<v-list-item-title v-for="itemdetail in item.subItem" :key="itemdetail.id" :value="itemdetail" >
<template v-if="itemdetail">
<v-list-item :prepend-icon="itemdetail.icon" :title="itemdetail.text"
active-color="teal-darken-1" rounded="xl">
</v-list-item>
</template>
</v-list-item-title>
</v-list-group>
</v-list>
</v-navigation-drawer>
</template>
<script>
export default {
data: () => ({
drawer: true,
items: [{
text: 'Parent Menu',
icon: 'mdi-pier-crane',
subItem: [{
text: 'Child Menu 1',
icon: 'mdi-engine'
},
{
text: 'Child Menu 2',
icon: 'mdi-calculator-variant'
},
{
text: 'Child Menu 3',
icon: 'mdi-list-status'
},
{
text: 'Child Menu 4',
icon: 'mdi-calendar-edit'
},
]
}, ],
}),
}
</script>
答案1
得分: 0
要使列表组默认打开或随任何操作而打开,请在列表组上使用v-model:opened
。只需按照以下步骤操作:
- 创建一个名为
open
的数据属性。 - 在列表元素上使用
v-model:opened="open"
。 - 将列表组与
text
属性绑定,如下所示:value="item.text"
。 - 由于列表组与
text
属性绑定,因此将所需项目的text
属性值分配给open
数据属性,即open: ['Parent Menu']
。
这是一个可工作的演示-
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-js -->
const { createApp } = Vue
const { createVuetify } = Vuetify
const vuetify = createVuetify()
const app = createApp({
data: () => ({
drawer: true,
open: ['Parent Menu'],
items: [{
text: 'Parent Menu',
icon: 'mdi-pier-crane',
subItem: [{
text: 'Child Menu 1',
icon: 'mdi-engine'
},
{
text: 'Child Menu 2',
icon: 'mdi-calculator-variant'
},
{
text: 'Child Menu 3',
icon: 'mdi-list-status'
},
{
text: 'Child Menu 4',
icon: 'mdi-calendar-edit'
},
]
}, ],
}),
}).use(vuetify).mount('#app')
<!-- language: lang-html -->
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@3.1.2/dist/vuetify.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vuetify@3.1.2/dist/vuetify.min.css">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@6.x/css/materialdesignicons.min.css" rel="stylesheet">
<div id="app">
<v-app>
<v-card
class="mx-auto"
width="300"
>
<v-list v-model:opened="open">
<v-list-item prepend-icon="mdi-home" title="Home"></v-list-item>
<v-list-group v-for="(item, i) in items" :key="i" :value="item.text" >
<template v-slot:activator="{ props }">
<v-list-item v-bind="props" :prepend-icon="item.icon" :title="item.text" active-color="orange-darken-1"
rounded="xl" ></v-list-item>
</template>
<v-list-item-title v-for="itemdetail in item.subItem" :key="itemdetail.id" :value="itemdetail.text" >
<v-list-item :prepend-icon="itemdetail.icon" :title="itemdetail.text"
active-color="teal-darken-1" rounded="xl">
</v-list-item>
</v-list-item-title>
</v-list-group>
</v-list>
</v-card>
</v-app>
</div>
<details>
<summary>英文:</summary>
To either open by default or open with any action, use `v-model:opened` on the list group. Just follow the below steps-
1. Create a data property named `open`.
1. Use `v-model:opened="open"` on your list element.
1. Bind list group with the `text` property, like this `:value="item.text"`
1. As list group is bonded with the `text` property, so assign the desired item's `text` property value to the `open` data property, i.e. `open: ['Parent Menu']`
Here is a working demo-
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-js -->
const { createApp } = Vue
const { createVuetify } = Vuetify
const vuetify = createVuetify()
const app = createApp({
data: () => ({
drawer: true,
open: ['Parent Menu'],
items: [{
text: 'Parent Menu',
icon: 'mdi-pier-crane',
subItem: [{
text: 'Child Menu 1',
icon: 'mdi-engine'
},
{
text: 'Child Menu 2',
icon: 'mdi-calculator-variant'
},
{
text: 'Child Menu 3',
icon: 'mdi-list-status'
},
{
text: 'Child Menu 4',
icon: 'mdi-calendar-edit'
},
]
}, ],
}),
}).use(vuetify).mount('#app')
<!-- language: lang-html -->
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@3.1.2/dist/vuetify.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vuetify@3.1.2/dist/vuetify.min.css">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@6.x/css/materialdesignicons.min.css" rel="stylesheet">
<div id="app">
<v-app>
<v-card
class="mx-auto"
width="300"
>
<v-list v-model:opened="open">
<v-list-item prepend-icon="mdi-home" title="Home"></v-list-item>
<v-list-group v-for="(item, i) in items" :key="i" :value="item.text" >
<template v-slot:activator="{ props }">
<v-list-item v-bind="props" :prepend-icon="item.icon" :title="item.text" active-color="orange-darken-1"
rounded="xl" ></v-list-item>
</template>
<v-list-item-title v-for="itemdetail in item.subItem" :key="itemdetail.id" :value="itemdetail.text" >
<v-list-item :prepend-icon="itemdetail.icon" :title="itemdetail.text"
active-color="teal-darken-1" rounded="xl">
</v-list-item>
</v-list-item-title>
</v-list-group>
</v-list>
</v-card>
</v-app>
</div>
<!-- end snippet -->
</details>
# 答案2
**得分**: 0
尝试这个方法:
```javascript
mounted() {
this.$nextTick(() => {
const groupElements = document.querySelectorAll(".v-list-group");
for (let i = 0; i < groupElements.length; i++) {
const group = groupElements[i];
const content = group.querySelector(".v-list-group__items");
if (content) {
group.classList.add("v-list-group--active");
content.style.display = "block";
}
}
});
},
英文:
Try this method:
mounted() {
this.$nextTick(() => {
const groupElements = document.querySelectorAll(".v-list-group");
for (let i = 0; i < groupElements.length; i++) {
const group = groupElements[i];
const content = group.querySelector(".v-list-group__items");
if (content) {
group.classList.add("v-list-group--active");
content.style.display = "block";
}
}
});
},
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论