<template>
  <el-menu
    default-active="1-4-1"
    class="el-menu-vertical-demo"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b"
    @close="handleClose"
    :collapse="isCollapse"
  >
    <h3>{{ isCollapse ? "后台" : "后台管理系统" }}</h3>
    <el-menu-item
      @click="clickMenu(item)"
      v-for="item in noChildren"
      :index="item.path"
      :key="item.path"
    >
      <i :class="'el-icon-' + item.icon"></i>
      <!-- 别把icon后的'-'漏掉了会失去图标 -->
      <span slot="title">{{ item.label }}</span>
    </el-menu-item>
  <div class="menu">
    <el-submenu v-for="item in hasChildren" :index="item.path" :key="item.path">
      <template slot="title">
        <i :class="'el-icon-' + item.icon"></i>
        <span slot="title">{{ item.label }}</span>
      </template>

      <el-menu-item-group
        v-for="(subItem, subIndex) in item.children"
        :key="subItem.path"
      >
        <el-menu-item @click="clickMenu(subItem)" :index="subIndex">
          <span>{{
          subItem.label
        }}</span>
        </el-menu-item>
      </el-menu-item-group>
    </el-submenu>
    </div>
  </el-menu>
</template>



<script>
export default {
  data() {
    return {
      menu: [],
      isCollapse: false,
    };
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },

    clickMenu(item) {
      console.log(item.url);
      this.$router.push({
        path: item.url,
      });
      // //  多写了一个$
      // this.$store.commit("selectMenu", item);
    },
  },
  created() {
    var that = this;
    this.$bus.$on("brotherEvent", (res) => {
      that.isCollapse = res;
    });
  },
  computed: {
    noChildren() {
      return this.asyncMenu.filter((item) => !item.children);
    },
    hasChildren() {
      return this.asyncMenu.filter((item) => item.children);
    },
    // isCollapse() {
    //   if (localStorage.getItem("isCollapse") == "true") {
    //     return true;
    //   } else {
    //     return false;
    //   }
    // },
    asyncMenu() {
      return [
        {
          path: "/",
          name: "home",
          label: "首页",
          icon: "s-home",
          url: "/pc_home",
        },
        {
          path: "/mall",
          name: "mall",
          label: "商品管理",
          icon: "video-play",
          url: "/pc_table",
        },
        {
          path: "/user",
          name: "user",
          label: "用户管理",
          icon: "user",
          url: "/pc_table",
        },
        {
          label: "其他",
          icon: "location",
          children: [
            {
              path: "/page1",
              name: "page1",
              label: "页面1",
              icon: "setting",
              url: "Other/PageOne",
            },
            {
              path: "/page2",
              name: "page2",
              label: "页面2",
              icon: "setting",
              url: "Other/PageTwo",
            },
          ],
        },
      ];
    },
  },
};
</script>
<style lang="less" scoped>
.el-menu--collapse  .el-submenu__title span{
    display: none;
  }
.el-menu-item {
  height: 40px;
  line-height: 40px;
  font-size: 12px;
}
::v-deep .el-submenu .el-submenu__title {
  height: 50px;
    line-height: 50px;
    font-size: 10px;
}

::v-deep .el-menu-item-group__title{
  padding:0px
}
::v-deep div.menu{
  .el-menu-item{
    background-color:rgb(70, 76, 81) !important
  }
  
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 150px;
  min-height: 100px;
}

.el-menu {
  height: 100%;
  border: none;
  h3 {
    color: #fff;
    text-align: center;
    line-height: 48px;
  }
}

</style>
如果要实现伸缩功能，事件总线中监听isCollapse这个变量并且赋值就可以了 //asyncMenu
解决了submenu收缩时的文字显示的问题。在收缩的状态把这玩意隐藏就好了

路由的格式
asyncMenu() {
      return [
        {
          path: "/",
          name: "home",
          label: "首页",
          icon: "s-home",
          url: "/pc_home",
        },
        {
          path: "/mall",
          name: "mall",
          label: "商品管理",
          icon: "video-play",
          url: "/pc_table",
        },
        {
          path: "/user",
          name: "user",
          label: "用户管理",
          icon: "user",
          url: "/pc_table",
        },
        {
          label: "其他",
          icon: "location",
          children: [
            {
              path: "/page1",
              name: "page1",
              label: "页面1",
              icon: "setting",
              url: "Other/PageOne",
            },
            {
              path: "/page2",
              name: "page2",
              label: "页面2",
              icon: "setting",
              url: "Other/PageTwo",
            },
          ],
        },
      ];
    }
