导航抽屉
v-navigation-drawer
组件是用户将用来浏览应用程序的组件。 导航抽屉已预先配置为可以在有或没有 vue-router 的情况下使用。 为了显示的目的,一些示例被包装在 v-card
元素中。 在您的应用程序中,通常会将 v-navigation-drawer
直接放置在 v-app
的下面。
如果使用启用了 app
属性的 v-navigation-drawer
,则不需要像示例中那样使用 absolute
属性。
用例
导航抽屉主要用于存放指向应用程序页面的链接。 将 null
用作其 v-model
的起始值会将抽屉初始化为在移动设备上关闭,在桌面上打开。 通常使用 nav 属性将抽屉与 v-list 组件配对。
template script
<template>
<v-card
height="400"
width="256"
class="mx-auto"
>
<v-navigation-drawer permanent>
<v-list-item>
<v-list-item-content>
<v-list-item-title class="title">
Application
</v-list-item-title>
<v-list-item-subtitle>
subtext
</v-list-item-subtitle>
</v-list-item-content>
</v-list-item>
<v-divider></v-divider>
<v-list
dense
nav
>
<v-list-item
v-for="item in items"
:key="item.title"
link
>
<v-list-item-icon>
<v-icon>{{ item.icon }}</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list>
</v-navigation-drawer>
</v-card>
</template>
<script>
export default {
data () {
return {
items: [
{ title: 'Dashboard', icon: 'mdi-view-dashboard' },
{ title: 'Photos', icon: 'mdi-image' },
{ title: 'About', icon: 'mdi-help-box' },
],
right: null,
}
},
}
</script>
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
实战场
template script
<template>
<v-container>
<v-row justify="space-around">
<v-col cols="12">
<v-select
v-model="color"
:items="colors"
label="Color"
></v-select>
</v-col>
<v-switch
v-model="drawer"
class="ma-2"
label="v-model"
></v-switch>
<v-switch
v-model="permanent"
class="ma-2"
label="Permanent"
></v-switch>
<v-switch
v-model="miniVariant"
class="ma-2"
label="Mini variant"
></v-switch>
<v-switch
v-model="expandOnHover"
class="ma-2"
label="Expand on hover"
></v-switch>
<v-switch
v-model="background"
class="ma-2"
label="Background"
></v-switch>
<v-switch
v-model="right"
class="ma-2"
label="Right"
></v-switch>
</v-row>
<v-card
height="400"
class="overflow-hidden"
>
<v-navigation-drawer
v-model="drawer"
:color="color"
:expand-on-hover="expandOnHover"
:mini-variant="miniVariant"
:right="right"
:permanent="permanent"
:src="bg"
absolute
dark
>
<v-list
dense
nav
class="py-0"
>
<v-list-item two-line :class="miniVariant && 'px-0'">
<v-list-item-avatar>
<img src="https://randomuser.me/api/portraits/men/81.jpg">
</v-list-item-avatar>
<v-list-item-content>
<v-list-item-title>Application</v-list-item-title>
<v-list-item-subtitle>Subtext</v-list-item-subtitle>
</v-list-item-content>
</v-list-item>
<v-divider></v-divider>
<v-list-item
v-for="item in items"
:key="item.title"
link
>
<v-list-item-icon>
<v-icon>{{ item.icon }}</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list>
</v-navigation-drawer>
</v-card>
</v-container>
</template>
<script>
export default {
data () {
return {
drawer: true,
items: [
{ title: 'Dashboard', icon: 'mdi-view-dashboard' },
{ title: 'Photos', icon: 'mdi-image' },
{ title: 'About', icon: 'mdi-help-box' },
],
color: 'primary',
colors: [
'primary',
'blue',
'success',
'red',
'teal',
],
right: false,
permanent: true,
miniVariant: false,
expandOnHover: false,
background: false,
}
},
computed: {
bg () {
return this.background ? 'https://cdn.vuetifyjs.com/images/backgrounds/bg-2.jpg' : undefined
},
},
}
</script>
示例
下面是一些简单到复杂的例子。
彩色的抽屉
导航抽屉可以定制,以适应任何应用程序的设计。这里,我们使用 append 插槽应用自定义背景颜色和附加的内容区域。
template script
<template>
<v-card
class="mx-auto"
height="400"
width="256"
>
<v-navigation-drawer
class="deep-purple accent-4"
dark
permanent
>
<v-list>
<v-list-item
v-for="item in items"
:key="item.title"
link
>
<v-list-item-icon>
<v-icon>{{ item.icon }}</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list>
<template v-slot:append>
<div class="pa-2">
<v-btn block>Logout</v-btn>
</div>
</template>
</v-navigation-drawer>
</v-card>
</template>
<script>
export default {
data () {
return {
items: [
{ title: 'Dashboard', icon: 'dashboard' },
{ title: 'Account', icon: 'account_box' },
{ title: 'Admin', icon: 'gavel' },
],
}
},
}
</script>
永久浮动抽屉
默认情况下,导航抽屉的右边框为 1px,将其与内容分开。 在此示例中,我们要从左侧拆下抽屉,让它自己浮动。 floating 属性删除右边框(如果使用 right,则删除左边框)。
template script
<template>
<v-card
class="pa-12"
color="indigo darken-2"
flat
>
<v-card
elevation="12"
width="256"
>
<v-navigation-drawer
floating
permanent
>
<v-list
dense
rounded
>
<v-list-item
v-for="item in items"
:key="item.title"
link
>
<v-list-item-icon>
<v-icon>{{ item.icon }}</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list>
</v-navigation-drawer>
</v-card>
</v-card>
</template>
<script>
export default {
data () {
return {
items: [
{ title: 'Home', icon: 'dashboard' },
{ title: 'About', icon: 'question_answer' },
],
}
},
}
</script>
迷你
当使用 mini-variant prop 时,抽屉会缩小(默认 56px),并隐藏 v-list
中除了第一个元素之外的所有内容。在这个例子中,我们使用了 .sync 修改器,它允许我们以程序化的方式绑定抽屉的扩展/收缩。
template script
<template>
<v-card>
<v-navigation-drawer
v-model="drawer"
:mini-variant.sync="mini"
permanent
>
<v-list-item class="px-2">
<v-list-item-avatar>
<v-img src="https://randomuser.me/api/portraits/men/85.jpg"></v-img>
</v-list-item-avatar>
<v-list-item-title>John Leider</v-list-item-title>
<v-btn
icon
@click.stop="mini = !mini"
>
<v-icon>mdi-chevron-left</v-icon>
</v-btn>
</v-list-item>
<v-divider></v-divider>
<v-list dense>
<v-list-item
v-for="item in items"
:key="item.title"
link
>
<v-list-item-icon>
<v-icon>{{ item.icon }}</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list>
</v-navigation-drawer>
</v-card>
</template>
<script>
export default {
data () {
return {
drawer: true,
items: [
{ title: 'Home', icon: 'mdi-home-city' },
{ title: 'My Account', icon: 'mdi-account' },
{ title: 'Users', icon: 'mdi-account-group-outline' },
],
mini: true,
}
},
}
</script>
临时的
临时抽屉位于其应用之上,并使用稀松布(叠加)来使背景变暗。这个抽屉的行为是模仿在移动设备时的持续抽屉。点击抽屉外部会导致关闭。
template script
<template>
<v-sheet
height="400"
class="overflow-hidden"
style="position: relative;"
>
<v-container class="fill-height">
<v-row
align="center"
justify="center"
>
<v-btn
color="pink"
dark
@click.stop="drawer = !drawer"
>
Toggle
</v-btn>
</v-row>
</v-container>
<v-navigation-drawer
v-model="drawer"
absolute
temporary
>
<v-list-item>
<v-list-item-avatar>
<v-img src="https://randomuser.me/api/portraits/men/78.jpg"></v-img>
</v-list-item-avatar>
<v-list-item-content>
<v-list-item-title>John Leider</v-list-item-title>
</v-list-item-content>
</v-list-item>
<v-divider></v-divider>
<v-list dense>
<v-list-item
v-for="item in items"
:key="item.title"
link
>
<v-list-item-icon>
<v-icon>{{ item.icon }}</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list>
</v-navigation-drawer>
</v-sheet>
</template>
<script>
export default {
data () {
return {
drawer: null,
items: [
{ title: 'Home', icon: 'dashboard' },
{ title: 'About', icon: 'question_answer' },
],
}
},
}
</script>
右侧
导航抽屉也可以位于应用程序(或元素)的右侧。 这对于创建带有辅助信息(可能没有任何导航链接)的侧页很有用。 使用 RTL 时,必须为抽屉明确定义 right。
template script
<template>
<v-card height="350px">
<v-navigation-drawer
absolute
permanent
right
>
<template v-slot:prepend>
<v-list-item two-line>
<v-list-item-avatar>
<img src="https://randomuser.me/api/portraits/women/81.jpg">
</v-list-item-avatar>
<v-list-item-content>
<v-list-item-title>Jane Smith</v-list-item-title>
<v-list-item-subtitle>Logged In</v-list-item-subtitle>
</v-list-item-content>
</v-list-item>
</template>
<v-divider></v-divider>
<v-list dense>
<v-list-item
v-for="item in items"
:key="item.title"
@click=""
>
<v-list-item-icon>
<v-icon>{{ item.icon }}</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list>
</v-navigation-drawer>
</v-card>
</template>
<script>
export default {
data () {
return {
items: [
{ title: 'Home', icon: 'mdi-home-city' },
{ title: 'My Account', icon: 'mdi-account' },
{ title: 'Users', icon: 'mdi-account-group-outline' },
],
}
},
}
</script>
Freelancer Free
A Free single page Vuetify theme for new developers.
ads by Vuetify
](https://store.vuetifyjs.com/product/freelancer-theme-free?ref=vuetifyjs.com)
悬停扩展
将组件置于 mini-variant 模式,并在悬停后展开。 不更改内容区域。 宽度可以通过 mini-variant-width 属性进行控制。
template script
<template>
<div class="ma-12 pa-12">
<template>
<v-card>
<v-navigation-drawer
permanent
expand-on-hover
>
<v-list>
<v-list-item class="px-2">
<v-list-item-avatar>
<v-img src="https://randomuser.me/api/portraits/women/85.jpg"></v-img>
</v-list-item-avatar>
</v-list-item>
<v-list-item link>
<v-list-item-content>
<v-list-item-title class="title">Sandra Adams</v-list-item-title>
<v-list-item-subtitle>sandra_a88@gmail.com</v-list-item-subtitle>
</v-list-item-content>
</v-list-item>
</v-list>
<v-divider></v-divider>
<v-list
nav
dense
>
<v-list-item link>
<v-list-item-icon>
<v-icon>mdi-folder</v-icon>
</v-list-item-icon>
<v-list-item-title>My Files</v-list-item-title>
</v-list-item>
<v-list-item link>
<v-list-item-icon>
<v-icon>mdi-account-multiple</v-icon>
</v-list-item-icon>
<v-list-item-title>Shared with me</v-list-item-title>
</v-list-item>
<v-list-item link>
<v-list-item-icon>
<v-icon>mdi-star</v-icon>
</v-list-item-icon>
<v-list-item-title>Starred</v-list-item-title>
</v-list-item>
</v-list>
</v-navigation-drawer>
</v-card>
</template>
</div>
</template>
<script>
export default {
//
}
</script>
背景
将自定义背景应用于抽屉。 如果您需要自定义 v-img
的属性,则可以使用 img 插槽。
template script
<template>
<v-card
class="mx-auto"
height="300"
width="300"
>
<v-navigation-drawer
absolute
dark
src="https://cdn.vuetifyjs.com/images/backgrounds/bg-2.jpg"
width="100%"
permanent
>
<v-list>
<v-list-item
v-for="([icon, text], i) in items"
:key="i"
link
>
<v-list-item-icon>
<v-icon>{{ icon }}</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>{{ text }}</v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list>
</v-navigation-drawer>
</v-card>
</template>
<script>
export default {
data: () => ({
items: [
['mdi-email', 'Inbox'],
['mdi-account-supervisor-circle', 'Supervisors'],
['mdi-clock-start', 'Clock-in'],
],
}),
}
</script>
组合抽屉
在此示例中,我们定义了一个自定义宽度来容纳我们的嵌套抽屉。 使用 v-row
,我们确保抽屉和列表在水平方向上彼此相邻。
template script
<template>
<v-card
class="mx-auto"
height="300"
width="330"
>
<v-navigation-drawer
permanent
width="100%"
>
<v-row class="fill-height" no-gutters>
<v-navigation-drawer
dark
mini-variant
mini-variant-width="56"
permanent
>
<v-list-item class="px-2">
<v-list-item-avatar>
<v-img src="https://randomuser.me/api/portraits/women/75.jpg"></v-img>
</v-list-item-avatar>
</v-list-item>
<v-divider></v-divider>
<v-list
dense
nav
>
<v-list-item
v-for="item in items"
:key="item.title"
@click=""
>
<v-list-item-action>
<v-icon>{{ item.icon }}</v-icon>
</v-list-item-action>
<v-list-item-content>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list>
</v-navigation-drawer>
<v-list class="grow">
<v-list-item
v-for="link in links"
:key="link"
link
>
<v-list-item-title v-text="link"></v-list-item-title>
</v-list-item>
</v-list>
</v-row>
</v-navigation-drawer>
</v-card>
</template>
<script>
export default {
data () {
return {
items: [
{ title: 'Home', icon: 'dashboard' },
{ title: 'About', icon: 'question_answer' },
],
links: ['Home', 'Contacts', 'Settings'],
mini: true,
}
},
}
</script>
底部抽屉
使用 bottom 属性,我们可以在移动设备上重新定位我们的抽屉,让它从屏幕的底部出来。这是另一种样式,只在遇到 mobile-break-point 时激活。
template script
<template>
<v-card
class="mx-auto overflow-hidden"
height="400"
width="344"
>
<v-system-bar color="deep-purple darken-3"></v-system-bar>
<v-app-bar
color="deep-purple accent-4"
dark
prominent
>
<v-app-bar-nav-icon @click.stop="drawer = !drawer"></v-app-bar-nav-icon>
<v-toolbar-title>My files</v-toolbar-title>
<v-spacer></v-spacer>
<v-btn icon>
<v-icon>mdi-magnify</v-icon>
</v-btn>
<v-btn icon>
<v-icon>mdi-filter</v-icon>
</v-btn>
<v-btn icon>
<v-icon>mdi-dots-vertical</v-icon>
</v-btn>
</v-app-bar>
<v-navigation-drawer
v-model="drawer"
absolute
bottom
temporary
>
<v-list
nav
dense
>
<v-list-item-group
v-model="group"
active-class="deep-purple--text text--accent-4"
>
<v-list-item>
<v-list-item-title>Foo</v-list-item-title>
</v-list-item>
<v-list-item>
<v-list-item-title>Bar</v-list-item-title>
</v-list-item>
<v-list-item>
<v-list-item-title>Fizz</v-list-item-title>
</v-list-item>
<v-list-item>
<v-list-item-title>Buzz</v-list-item-title>
</v-list-item>
</v-list-item-group>
</v-list>
</v-navigation-drawer>
<v-card-text>
The navigation drawer will appear from the bottom on smaller size screens.
</v-card-text>
</v-card>
</template>
<script>
export default {
data: () => ({
drawer: false,
group: null,
}),
watch: {
group () {
this.drawer = false
},
},
}
</script>