vue 多级导航菜单

news2024/10/5 20:19:58

目录,三个文件

 

tree-node.vue

<template>
  <div class="tree-node-container">
    <node-content></node-content>
    <div
      class="tree-node-children"
      :style="{
        paddingLeft: indent
      }"
      v-if="nextShow">
      <tree-node
        v-for="(child, idx) of nodeData.children"
        :nodeData="child"
        :indent="indent"
        :key="idx">
      </tree-node>
    </div>
  </div>
</template>

<script>

export default {
  name: 'tree-node',
  props: {
    nodeData: {
      type: Object,
      required: true
    }
  },
  components: {
    'node-content': {
      render (h) {

        let slot = this.$parent.tree.$slots.default
        let { nodeData, parentData, level, nextShow } = this.$parent
        return (slot ? slot({ parentData, data: nodeData, level, nextShow }) : '<div>未定义插槽内容</div>')
      }
    }
  },
  data () {
    return {
      tree: false,
      level: 0,
      parentData: null,
      childrenShow: true,
      indent: undefined
    }
  },
  computed: {
    nextShow () {
      return this.nodeData.children && this.nodeData.children.length && this.childrenShow
    }
  },
  created () {
    let parent = this.$parent
    if (parent.isTree) {
      this.level = 1
    } else {
      this.level = parent.level + 1
      this.parentData = parent.nodeData
    }
    while (parent && !parent.isTree) {
      parent = parent.$parent
    }
    this.tree = parent
    this.indent = this.tree.indent
    console.log('this.nodeData',this.nodeData)
    this.tree.registerNodeComponent(this.nodeData.id, this)

  },
  beforeDestroy () {
    console.log('pppppppppppppppppppppp')
    this.tree.removeNodeComponent(this.nodeData.id)
  },
  mounted(){
    // this.tree.removeNodeComponent(this.nodeData.id)
  },
  methods: {
    showChildren (show) {
      this.childrenShow = show
    }
  }
}
</script>

 tree.vue

<template>
  <div class="tree-container">
    <tree-node
      v-for="(nodeData, idx) of treeData"
      :nodeData="nodeData"
      :key="idx">
    </tree-node>
  </div>
</template>

<script>
import treeNode from './tree-node'

export default {
  components: {
    treeNode
  },
  props: {
    treeData: {
      type: Array,
      requied: true
    },
    indent: {
      type: String,
      default: '20px'
    }
  },
  data () {
    return {
      isTree: true,
      level: 0,
      componentMap: {},
      width:'200px',
      height:'500px'

    }
  },
  methods: {
    registerNodeComponent (id, component) {
      this.componentMap[id] = component
    },
    removeNodeComponent (id) {
      console.log('this.componentMap',this.componentMap,id)
      this.componentMap[id] = undefined
    },
    showChildren (id, show) {
      this.componentMap[id] && this.componentMap[id].showChildren(show)
    }

  },
  mounted(){
    console.log('this',this.componentMap)
  }
}
</script>

<style  scoped>
.tree-container{
   text-align:left
}

</style>

使用

Tree.vue

<template>
  <div class="tree-page">
    <div class="tree">
      <tree :treeData="treeData" ref="tree">
        <template v-slot="{ parentData, data, level, nextShow }">
          <div class="node-slot">

            <div class="slot-content"
            :class="{active_content:activeId === data.id}"
             @click="clickTitle(data)">
              <img :src="data.icon" />
              <span class="node-title">{{ data.title }}</span>

            </div>
            <div class="expand">
              <div
                class="expand-btn"
                @click.stop="clickExpand(data, nextShow)"
                v-if="data.children && data.children.length"
              >
              <span v-if="nextShow">+</span>
              <span v-else>-</span>

              </div>

            </div>
          </div>
        </template>
      </tree>
    </div>
  </div>
</template>

<script>
import tree from "../components/tree/tree.vue";

export default {
  name: "app",
  components: {
    tree,
  },
  data() {
    return {
      treeData:[
          {
            title: "第一个",
            icon: "icons/上升.svg",
            id: "1",
            children: [
              { title: "第一/1个", icon: "icons/上升.svg", id: "1-1" },
              {
                title: "第一/2个",
                icon: "icons/上升.svg",
                id: "1-2",
                children: [
                  { title: "第一ce个", icon: "icons/上升.svg", id: "1-2-1" },
                ],
              },
            ],
          },
          { title: "第二个", icon: "icons/上升.svg", id: "2" },
          { title: "第三个", icon: "icons/上升.svg", id: "3" },
        ],
      nodeId: 100,
      activeId:''
    };
  },
  methods: {
    clickExpand(data, nextShow) {
      this.$refs.tree.showChildren(data.id, !nextShow);
    },

    clickTitle(data){
      this.activeId = data.id
    }
  },
};
</script>
<style scoped>
.tree-page {
  display: flex;
  width: 200px;
}
.tree {
  flex: auto;
  overflow: auto;
  padding: 20px;
}
.node-slot {
  display: flex;
  align-items: center;
  line-height: 2;
  font-size: 20px;
}
.expand {
  flex-shrink: 0;
  width: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 2px;
}
.expand-btn {
  font-size: 24px;
  color: #888;
  line-height: 1;
  border-radius: 50%;
  cursor: pointer;
}
.dot {
  border-radius: 50%;
  border: 3px solid #aaa;
  width: 0;
}
.slot-content {
  flex: auto;
  padding: 0 4px;
}
.slot-content:hover {
  background: rgba(64, 158, 255, 0.2);
}
.node-title {
  vertical-align: middle;
}
.node-menu-list {
  font-size: 14px;
  vertical-align: middle;
  margin-left: 10px;
}
.menu {
  padding: 2px 6px;
  margin: 0 2px;
}
.active_content{
  color: red;
  background: rgba(64, 158, 255, 0.2);
}
img{
  width: 16px;
  height:16px;
  background-color: blue;
}
</style>

以上参照了这位的博客Vue树组件实现 - 沐码小站

下面是用element 抽屉封装的,感觉没那么灵活

两个文件

menuChild.vue 

<template>
  <el-collapse class="coll-wrap">
    <div
      v-for="(item, i) in list"
      :key="item.id"
      :style="{
        'padding-left': deep + '0px',
      }"
      @click="menuClick(item)"
    >
      <el-collapse-item v-if="item.children" :title="item.title" :name="i">
        <template #title>
          <img :src="item.icon" />
          <span> {{ item.title }}</span>
        </template>
        <SubMenu
          :labelData="labelData"
          @updateLabel="updateLabel"
          :deep="deep + 1"
          v-if="item.children"
          :list="item.children"
        ></SubMenu>
      </el-collapse-item>
      <div class="item" :class="{ active: labelData == item.id }" v-else>
        <img :src="item.icon" /><span> {{ item.title }}</span>
      </div>
    </div>
  </el-collapse>
</template>

<script>
// import SubMenu from "./menu.vue"
export default {
  name: "SubMenu",
  data() {
    return {
      activeName: "",
      // list:[
      //   {title:'第一个',icon:"icons/上升.svg",id:'1',children:[
      //     {title:'第一/1个',icon:"icons/上升.svg",id:'1-1'},
      //     {title:'第一/2个',icon:"icons/上升.svg",id:'1-2'}
      //   ]},
      //   {title:'第二个',icon:"icons/上升.svg",id:'2'},
      //   {title:'第三个',icon:"icons/上升.svg",id:'3'}
      // ]
    };
  },
  props: {
    list: {
      type: Array,
      default() {
        return [
          {
            title: "第一个",
            icon: "icons/上升.svg",
            id: "1",
            children: [
              { title: "第一/1个", icon: "icons/上升.svg", id: "1-1" },
              {
                title: "第一/2个",
                icon: "icons/上升.svg",
                id: "1-2",
                children: [
                  { title: "第一ce个", icon: "icons/上升.svg", id: "1-2-1" },
                ],
              },
            ],
          },
          { title: "第二个", icon: "icons/上升.svg", id: "2" },
          { title: "第三个", icon: "icons/上升.svg", id: "3" },
        ];
      },
    },

    labelData: {
      type: String,
      default() {
        return "";
      },
    },
    deep: {
      type: Number,
      default() {
        return 0;
      },
    },
  },
  // components:{
  // SubMenu
  // },
  mounted() {
    console.log(this.list);
  },
  methods: {
    updateLabel(item) {
      this.$emit("updateLabel", item);
    },
    menuClick(item) {
      if (!item.children) {
        this.activeName = item.id;
        console.log("item", item);
        this.$emit("updateLabel", item);
      }
    },
  },
};
</script>

<style scoped>
.coll-wrap /deep/ .el-collapse-item__content {
  padding-bottom: 0 !important;
}
.coll-wrap /deep/ .el-collapse-item__wrap {
  border: 0 !important;
}
.item {
  padding: 15px 0;
  background-color: aqua;
}
.active {
  background-color: aliceblue;
}
img {
  width: 16px;
  height: 16px;
  background-color: blue;
}
.el-collapse {
  border: 0 !important;
  background-color: aqua;
}
.el-collapse-item /deep/ .el-collapse-item__header {
  border: 0 !important;
  background-color: aqua;
}
</style>

menu.vue

<template>
<MenuChild
class="my_menu"
:style="{
width:width,
height:height,

}"
:labelData="labelData"
@updateLabel = "updateLabel"
></MenuChild>
</template>

<script>
import MenuChild from "./menuChild.vue"
export default {
data(){
  return{
    labelData:'',
    width:'200px',
      height:'500px'
  }
},
components:{
  MenuChild

},
methods:{
  updateLabel(item){
    console.log(item)
    this.labelData = item.id
  }
}
}
</script>

<style scoped>
.my_menu{
  background-color: aqua;
}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/727927.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

力扣 47. 全排列 II

题目来源&#xff1a;https://leetcode.cn/problems/permutations-ii/description/ C题解1&#xff1a;这道题需要有两个不同的数组来标记使用过的元素&#xff0c;一个存放同层使用过的元素&#xff0c;根据元素数值来判断&#xff0c;范围为[-10,10]&#xff0c;在同层更新即…

95、基于STM32单片机烟雾温度报警器设计火灾防火检测可燃气体监测系统设计(程序+原理图+任务书+参考论文+开题报告+元器件清单等)

摘 要 本设计采用使用STM32F103作为控制器件&#xff0c;MQ-2型半导体可燃气体敏感元件烟雾传感器作为检测烟雾器件&#xff0c;DS18B20温度传感器作为温度检测器件&#xff0c;蜂鸣器和电源指示灯作为报警器件&#xff0c;继电器作为水泵的控制器件。烟雾报警器主要由烟雾信…

动态多目标优化算法:基于自适应启动策略的混合交叉动态约束多目标优化算法(MC-DCMOEA)求解CEC2015(提供MATLAB代码)

一、动态多目标优化问题 1.1问题定义 1.2 动态支配关系定义 二、 基于自适应启动策略的混合交叉动态多目标优化算法 基于自适应启动策略的混合交叉动态多目标优化算法&#xff08;Mixture Crossover Dynamic Constrained Multi-objective Evolutionary Algorithm Based on S…

mysql sysdate()不走索引问题解析

1.常规比较 结论: SYSDATE()返回执行时间,而NOW()返回时间常量 2.索引比较 结论: 因为SYSDATE()是非确定性的,索引不能用于评估求值引用它的表达式,所以不走索引

隐语1.0正式发布|MVP部署体验包、资源调度框架Kuscia全新亮相!

2023 年 7 月 7 日&#xff0c;在世界人工智能大会组委会办公室指导下&#xff0c;隐语开源社区携手蚂蚁集团和机器之心共同主办的数据要素与隐私计算论坛在上海世博会议中心举行。论坛上&#xff0c;蚂蚁集团隐私计算部总经理、隐语社区负责人王磊发布了隐语 1.0 版本&#xf…

java项目之美食推荐管理系统(ssm+mysql+jsp)

风定落花生&#xff0c;歌声逐流水&#xff0c;大家好我是风歌&#xff0c;混迹在java圈的辛苦码农。今天要和大家聊的是一款基于ssm的美食推荐管理系统。 开发环境&#xff1a; 后端&#xff1a; 开发语言&#xff1a;Java 框架&#xff1a;ssm&#xff0c;mybatis JDK版…

docker环境部署postgres版本nacos

1、docker安装postgres 执行命令&#xff1a;docker pull postgres 拉取最新版postgres 2、查看postgres镜像是否安装成功: docker imags(查看镜像)&#xff0c;可以看到已经拉取到了最新版本 的postgres镜像 3、编辑一个docker-compose.yml文件&#xff0c;账号是postgres&a…

信息化建设的需求下,企业如何部署管理平台?并保障其应用落地?

系统部署已久&#xff0c;为何始终无法落地应用&#xff1f; 随着信息化建设的快速发展&#xff0c;企业高效管理的需求日益增长。传统IT服务缺乏统一管理平台&#xff0c;服务流程不够灵活&#xff0c;运维工作效率低下&#xff0c;亟需一个灵活、高效的运维管理模式。 而在认…

大禹智库:下一代向量数据库——具备可视化的AI 原生云向量数据库

日前&#xff0c;业界正式发布国内首个 具备可视化和自动化的AI 原生云向量数据库 " 据悉&#xff0c;向量数据库专门用于存储和查询向量数据&#xff0c;业界称之为大模型的 " 海马体 "&#xff0c;可解决大模型预训练成本高、没有 " 长期记忆 "、知…

Spring Mvc:初识SpringMvc

编译软件&#xff1a;IntelliJ IDEA 2019.2.4 x64 操作系统&#xff1a;win10 x64 位 家庭版 Maven版本&#xff1a;apache-maven-3.6.3 Mybatis版本&#xff1a;3.5.6 SpringMvc版本&#xff1a;5.3.1 文章目录 一、SpringMvc是什么&#xff1f;二、SpringMvc是如何执行业务流…

OpenGL入门教程——PT1

由于自己不是计算机图形学出身&#xff0c;所以采纳了一些教材、博客、GPT的综合答案&#xff0c;尽可能作为一篇认识图形学&#xff0c;学会OpenGL简单函数库的博客&#xff01;多多包涵 我会从最基础的显示相关知识逐步导入OpenGL 一、屏幕是如何工作的&#xff0c;为什么电…

Apache Doris 0.15.3升级至1.1.5避坑实用总结

概述&#xff1a; ​ Apache Doris 从0.15到1.1.5属于一个大版本升级&#xff0c;部分语法和使用方式也发生了较大的变化&#xff0c;本文除前期兼容性调研&#xff08;可以通过官网查到到一部分兼容性问题&#xff09;已知问题外&#xff0c;生产环境正式升级之后遇到的突发问…

【Web 安全】刚开始学渗透,零基础怎么入门?

上周旁听了一个大学学长组织的线上网络安全交流会&#xff0c;里边不乏充斥着各位行业大牛&#xff0c;讲的内容确实精彩&#xff0c;可能对于网络安全经验 5 年的人来说&#xff0c;是受益匪浅&#xff0c;欢迎程度极高&#xff0c;恨不得跳出屏幕来表示赞同&#xff0c;毕竟很…

2022年06月份青少年软件编程Scratch图形化等级考试试卷三级真题(含答案)

2022-06 Scratch三级真题 分数&#xff1a;100 题数&#xff1a;38 测试时长&#xff1a;60min 一、单选题(共25题&#xff0c;共50分) 1.点击绿旗&#xff0c;舞台上的角色会说出&#xff1f;&#xff08; &#xff09;(2分) A. 2022年5月1日 B. 1日5月2022年 C. 2022…

【2023最全教程】性能测试基础知识学习路线(建议收藏)

本文介绍一下性能测试的基础内容和一些学习经验&#xff0c;主要讲针对服务器端的性能测试。其他代码级性能测试、前端性能测试等属于比较细分的领域&#xff0c;建议大家有需要的时候针对性得去学。而对于服务器端的性能测试&#xff0c;即使是不做性能测试的人&#xff0c;最…

如何利用GIS数据转换器-栅格 实现去云

GIS数据转换器-栅格不仅能够实现数据转换、数据分割、数据。还可以实现简单去云。 首先打开一副LANDSAT的数据 然后查看云的值范围&#xff0c;值大约为&#xff1a;20000-45000之间 然后在数据转换器中设置设置&#xff0c;过滤范围 点转换按钮进行转换,这样云位置的像素就被…

SpringBoot教学资料3-SpringBoot启动常见问题

java.lang.RuntimeException: java.lang.RuntimeException: org.codehaus.plexus.component.repository.exc Maven版本过高 &#xff0c;与你使用的IDEA版本不兼容。 推荐版本&#xff1a;maven3.6(建议)/maven3.9 控制台Process terminated https://blog.csdn.net/weixin_4458…

SMALE周报_20230707

目录标题 1. 上周回顾2. 本周计划3. 完成情况3.1. 背景3.2. 不确定性类别3.3. 具体方法3.3.1. Aleatoric Uncertainty3.3.1. Epistemic Uncertainty 4. 存在的主要问题5. 下一步工作 1. 上周回顾 这周开始写周报&#xff0c;此项为空。 2. 本周计划 总结不确定性在神经网络中…

96、基于STM32单片机的温湿度DHT11 烟雾火灾报警器蓝牙物联网APP远程控制设计(程序+原理图+任务书+参考论文+开题报告+流程图+元器件清单等)

单片机及温湿度、烟雾传感器是烟雾报警器系统的两大核心。单片机好比一个桥梁&#xff0c;联系着传感器和报警电路设备。近几年来&#xff0c;单片机已逐步深入应用到工农业生产各部门及人们生活的各个方面。各种类型的单片机也根据社会的需求而开发出来。单片机是器件级计算机…

试题小结2

1.内部类和子类的区别 内部类是指在一个外部类的内部再定义一个类&#xff0c;内部类对外部类有访问权限&#xff0c;可以访问类中定义的所有变量和方法。 子类是从父类中继承的类&#xff0c;子类可以访问父类所有public和protected的属性和方法。 Java语言有四个权限访问修…