element-plus侧边栏折叠
时间: 2023-12-01 07:43:06 浏览: 195
element-plus(element-plus@2.8.1/element-plus-2.8.1) 本地离线资源
以下是使用element-plus实现侧边栏折叠的示例代码:
```html
<template>
<div class="container">
<el-aside width="200px" style="background-color: rgb(238, 241, 246)">
<el-menu default-active="1" class="el-menu-vertical-demo" :collapse="isCollapse">
<el-submenu index="1">
<template slot="title">
<i class="el-icon-location"></i>
<span>导航一</span>
</template>
<el-menu-item-group>
<template slot="title">分组一</template>
<el-menu-item index="1-1">选项1</el-menu-item>
<el-menu-item index="1-2">选项2</el-menu-item>
</el-menu-item-group>
<el-menu-item-group title="分组2">
<el-menu-item index="1-3">选项3</el-menu-item>
</el-menu-item-group>
<el-submenu index="1-4">
<template slot="title">选项4</template>
<el-menu-item index="1-4-1">选项4-1</el-menu-item>
</el-submenu>
</el-submenu>
<el-menu-item index="2">
<i class="el-icon-menu"></i>
<span slot="title">导航二</span>
</el-menu-item>
<el-menu-item index="3">
<i class="el-icon-setting"></i>
<span slot="title">导航三</span>
</el-menu-item>
</el-menu>
</el-aside>
<el-main>
<el-button type="primary" @click="toggleCollapse">{{isCollapse ? '展开' : '折叠'}}</el-button>
</el-main>
</div>
</template>
<script>
export default {
data() {
return {
isCollapse: false
}
},
methods: {
toggleCollapse() {
this.isCollapse = !this.isCollapse
}
}
}
</script>
<style>
.container {
display: flex;
height: 100vh;
}
.el-aside {
height: 100%;
overflow: hidden;
}
.el-menu-vertical-demo {
height: 100%;
}
.el-main {
flex: 1;
padding: 20px;
}
</style>
```
在这个示例中,我们使用了`el-aside`和`el-menu`组件来实现侧边栏,其中`el-menu`组件的`collapse`属性用于控制菜单的折叠状态。我们通过点击按钮来切换`isCollapse`的值,从而实现侧边栏的折叠和展开。
阅读全文