vue处理侧边栏的展开和收起功能

处理侧边栏的展开和收起功能

->(第一步:在vuex的state里面定义侧边栏的宽度)

(*store/index.js实现代码如下) `

const store = createStore({
    state() {
       return {
         //侧边栏的宽度
         asideWidth:"250px",
       }
    },
    mutations:{
        // 定义展开或者收起侧边栏的状态
        handleAsideWidth(state) {
        state.asideWidth = state.asideWidth === "250px"  ?  "64px" : "250px" 
      }
    }
})

`

->(第二步:引入到Header.vue组件中)

(*代码如下)

`

<template>
   <!-- 收起或者展开状态 -->
   <el-icon class="icon-btn" @click="$store.commit('handleAsideWidth')">
   <!--展开图标-->
   <fold v-if="$store.state.asideWidth === '250px'"/>
   <!--收起图标-->
   <Expand v-else/>
   </el-icon>
</template>

`

->(第三步:在引入到Menu.vue组件中)

(*代码如下) `

<template>
  <div class="f-menu" :style="{   width:$store.state.asideWidth }">
   <!-- collapse:侧边栏是否展开 -->
  <el-menu unique-opened  :collapse="isCollapse" default-active="2" class="border-0" router :collapse-transition="false">
  </el-menu>
 </div>
</template>

<!--script部分-->
 <script>
  import { computed } from 'vue'
  import { useStore } from 'vuex'

 //是否折叠
 const store = useStore()

 const isCollapse = computed(() => !(store.state.asideWidth === '250px'))
</script>

<!--样式部分-->

 <style>
 .f-menu {
    /* 加上动画 */
    transition: all 0.2s;
    width: 250px;
    top: 64px;
    bottom: 0;
    left: 0;
    overflow-y: auto;
    overflow-x:hidden;
    @apply shadow-md fixed bg-light-50;
 }
 </style>

`

->(第四步:改变主屏的大小)

(*代码如下) 在src/Layout下的index.vue中 `

<template>
<el-aside :width="$store.state.asideWidth">
<!-- 侧边栏 -->
<Menu></Menu>
</el-aside>
</template>

<!-- 添加一个动画的效果 -->
<style scoped>
  .el-side {
    transition: all 0.2s;
  }
</style>

`

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值