elementui 实现左侧三级联动导航栏

<template>
  <div class="layout_left">
    <el-menu
      class="el-menu-vertical-demo"
      @open="handleOpen"
      @close="handleClose"
      background-color="#324157"
      :default-active="onRoutes"
      text-color="#bfcbd9"
      active-text-color="#20a0ff"
      :collapse="isCollapse"
      router
    >
      <template v-for="one in items">
        <template v-if="one.subs">
          <el-submenu
            :index="one.index"
            :key="one.index"
          >
            <template slot="title">
              <i :class="one.icon"></i>
              <span>{{one.title}}</span>
            </template>
            <template v-for="two in one.subs">
              <template v-if="two.children">
                <el-submenu
                  :key='two.index'
                  :index="two.index"
                >
                  <template slot="title">
                    <span>{{two.title}}</span>
                  </template>
                  <template v-for="children in two.children">
                    <el-menu-item
                      :key='children.index'
                      :index='children.index'
                    >{{children.title}}</el-menu-item>
                  </template>
                </el-submenu>
              </template>
              <template v-else>
                <el-menu-item-group :key='two.index'>
                  <el-menu-item :index='two.index'>{{two.title}}</el-menu-item>
                </el-menu-item-group>
              </template>
            </template>
          </el-submenu>
        </template>
        <template v-else>
          <el-menu-item
            :index="one.index"
            :key="one.index"
          >
            <template>
              <i :class="one.icon"></i>
              <span>{{one.title}}</span>
            </template>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>



<script>
import bus from "@/common/bus";
export default {
  data() {
    return {
      isCollapse: false,
      items: [
        {
          icon: "el-icon-s-open",
          index: "/",
          title: "首页"
        },
        {
          icon: "el-icon-rank",
          index: "2",
          title: "用户数据",
          subs: [
            {
              index: "data1",
              title: "数据1",
              children: [
                {
                  index: "addata1",
                  title: "添加数据1"
                }
              ]
            },
            {
              index: "data2",
              title: "数据2"
            },
            {
              index: "data3",
              title: "数据3"
            }
          ]
        },
        {
          icon: "el-icon-date",
          index: "3",
          title: "组件间传值",
          subs: [
            {
              index: "zujian1",
              title: "方法一"
            },
            {
              index: "zujian2",
              title: "方法二"
            },
            {
              index: "zujian3",
              title: "方法三"
            }
          ]
        }
      ]
    };
  },
  computed: {
    onRoutes() {
      return this.$route.path.replace("/", "");
    }
  },
  created() {
    bus.$on("collapse", msg => {
      this.isCollapse = msg;
    });
  },
  methods: {
    handleOpen(key, keyPath) {},
    handleClose(key, keyPath) {}
  }
};
</script>


<style lang="less" scoped>
.layout_left {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
  background-color: #324157;
}
.layout_left::-webkit-scrollbar {
  width: 0;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 250px;
}
.layout_left > ul {
  height: 100%;
}
</style>

实现效果

在这里插入图片描述

在这里插入图片描述

你可以使用 Vue 和 Element UI 来实现下拉框的二级联动。下面是一个简单的示例: 首先,在你的 Vue 组件中引入相关的库和组件: ```javascript <template> <div> <el-select v-model="selectedProvince" @change="handleProvinceChange"> <el-option v-for="province in provinces" :key="province" :label="province" :value="province"></el-option> </el-select> <el-select v-model="selectedCity"> <el-option v-for="city in cities" :key="city" :label="city" :value="city"></el-option> </el-select> </div> </template> <script> export default { data() { return { selectedProvince: '', selectedCity: '', provinces: ['省份1', '省份2', '省份3'], // 替换成你的省份数据 cities: { '省份1': ['城市1', '城市2', '城市3'], // 替换成你的城市数据 '省份2': ['城市4', '城市5', '城市6'], '省份3': ['城市7', '城市8', '城市9'] } }; }, methods: { handleProvinceChange() { this.selectedCity = ''; // 清空已选择的城市 } } }; </script> ``` 在上面的示例中,我们使用了两个 `el-select` 组件来展示省份和城市的下拉框。`provinces` 数组存储了所有的省份数据,`cities` 对象存储了每个省份对应的城市数据。当选择省份时,`handleProvinceChange` 方法会被调用,清空已选择的城市并更新城市下拉框的选项。 请注意,上述示例中的省份和城市数据都是静态的,你需要根据实际情况进行替换。你可以从后端或其他数据源中获取数据,并将其动态地传递给组件。 希望这个示例能帮助到你实现下拉框的二级联动!如有任何问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值