笔记三十二:vue使用递归实现多级目录数

74 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

本文已参与「新人创作礼」活动,一起开启掘金创作之路。 在这里插入图片描述 APP.vue:


<template>
    <div id="app">
        <FilePathTree></FilePathTree>
    </div>
</template>
 
<script>
import FilePathTree from "@/components/FilePathTree.vue";
export default {
    name: "Aside",
    data() {
        return {
        };
    },
    components: {
        FilePathTree
    },
   
};
</script>
 
<style lang="scss">
</style>

FilePathTree.vue:


<template>
    <div>
        <ul class="child-infoBox">
            <li
                v-for="(item, index) in asideList"
                v-bind:key="index"
                class="first-con"
            >
                <div
                    class="main-title first-title"
                    :class="{'isActiveFile':$store.state.checkModel==item.id}"
                    @click.prevent="mainDropDown(item)"
                >
                    <div class="" :class="['file-img',item.showFlag?'file-icon2':'file-icon1']"></div>
                    <span class="title">{{ item.title }}</span>
                </div>
                <ul
                    v-if="item.children"
                    class="child-infoBox"
                    v-show="item.showFlag"
                >
                    <FreeItem :asideChildren="item.children"></FreeItem>
                </ul>
            </li>
        </ul>
    </div>
</template>
 
<script>
import FreeItem from "@/components/FilePathTreeItem.vue";
export default {
    data() {
        return {
            asideList: [
                {
                    id:"1",
                    title: "1级标题",
                    showFlag: false,
                    children: [
                        {
                            id:"1-1",
                            title: "1-1级标题",
                            showFlag: false,
                            children: [
                                {
                                    id:"1-1-1",
                                    title: "1-1-1三级标题",
                                    showFlag: false,
                                    children: [
                                        {
                                            id:"1-1-1-1",
                                            title: "1-1-1-1三级标题",
                                            showFlag: false,
                                        },
                                    ],
                                },
                                {
                                    id:"1-1-2",
                                    title: "1-1-2级标题",
                                    showFlag: false,
                                },
                                {
                                    id:"1-1-3",
                                    title: "1-1-3级标题",
                                    showFlag: false,
                                },
                            ],
                        },
                        {
                            id:"1-2",
                            title: "1-2级标题",
                            showFlag: false,
                            children: [
                                {
                                    id:"1-2-1",
                                    title: "1-2-1三级标题",
                                    showFlag: false,
                                },
                            ],
                        },
                        { id:"1-3",title: "1-3级标题", showFlag: false },
                    ],
                },
                {
                    id:"2",
                    title: "2级标题",
                    showFlag: false,
                    children: [
                        {
                            id:"2-1",
                            title: "2-1级标题",
                            showFlag: false,
                            children: [
                                {
                                    id:"2-1-1",
                                    title: "2-1-1级标题",
                                    showFlag: false,
                                },
                                {
                                    id:"2-1-2",
                                    title: "2-1-2级标题",
                                    showFlag: false,
                                },
                                {
                                    id:"2-1-3",
                                    title: "2-1-3级标题",
                                    showFlag: false,
                                },
                            ],
                        },
                        { id:"2-2",title: "2-2级标题", showFlag: false },
                        { id:"2-3",title: "2-3级标题", showFlag: false },
                    ],
                },
            ],
        };
    },
    components: {
        FreeItem,
    },
    methods: {
        mainDropDown(item) {
            item.showFlag = !item.showFlag;
            this.$store.commit("updateClickModel",item.id)
        },
    },
};
</script>
 
<style lang="scss">
.isActiveFile{
   background-color: rgba($color: #2ebebb, $alpha: 0.16);
}
ul {
    margin: 0;
    padding: 0;
    margin-left: 35px;
    color: #4f5959;
    list-style-type: none;
}
li {
    cursor: pointer;
    position: relative;
    //  border-left: 1px solid #00aeaa;
    padding-top: 20px;
    &.first-con {
        border-left: 0;
        &::after {
            content: "";
            position: absolute;
            top: 0;
            width: 0px !important;
            height: 100%;
            background-color: #00aeaa;
        }
    }
    &:last-child {
        &::after {
            content: "";
            position: absolute;
            top: -14px;
            width: 1px;
           height: 58px;
        }
    }
    &::after {
        content: "";
        position: absolute;
        top: -14px;
        width: 1px;
        height: 100%;
        background-color: #00aeaa;
    }
}
.file-title{
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 48px;
    padding-left: 6px;
        .left-tit{
        flex: 1;
        display: flex;
        align-items: center;
    }
}
.first-title {
    padding-left: 22px;
}
.file-img {
    width: 26px;
    height: 20px;
    margin-right: 20px;
}
.file-icon1 {
    background: url("../assets/img/file-close.png") no-repeat center center;
    background-size: contain;
}
.file-icon2 {
    background: url("../assets/img/file-open.png") no-repeat center center;
    background-size: cover;
}
.main-title {
    height: 48px;
    font-size: 14px;
    display: flex;
    align-items: center;
}
.main-title .iconfont {
    margin-left: 15px;
}
.first-infoBox {
}
.line {
    width: 22px;
    height: 1px;
    background-color: #00aeaa;
}
</style>

FilePathTreeItem.vue

<template>
	<div >
		<li v-for="(item,index) in asideChildren" v-bind:key="index" >
			<div class="main-title" @click.prevent="secondaryDropDown(item)"   >
                <div class="line"></div>
                <div class="file-title" :class="{'isActiveFile':$store.state.checkModel==item.id}">
                    <div class="left-tit">
                        <div class="" :class="['file-img',item.showFlag?'file-icon2':'file-icon1']"></div>
                        <span>{{item.title}}</span>
                    </div>
                </div>
			</div>
			<ul v-if="item.children" v-show="item.showFlag" class="child-infoBox">
				<FreeItem :asideChildren="item.children"></FreeItem>
			</ul>			
		</li>
	</div>
</template>
 
<script>
	export default{
		name: 'FreeItem',
		props: {
			asideChildren: {
				type: Array
			}
		},
        data(){
            return{
                checkIsActive:"",
            }
        },
		created(){
			console.log(this.asideChildren);
		},
        
        methods:{
            secondaryDropDown(item){
                if(item.children){
                    item.showFlag=!item.showFlag
                }
                   this.$store.commit("updateClickModel",item.id)
            }
        }
	}
</script>
 
<style scoped="scoped">

</style>


vueX:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
      checkModel:""//选择的模型
  },
  mutations: {
    //   修改模型
    updateClickModel(state,info) {
        state.checkModel=info
    },
  },
  actions: {
  },
  modules: {
  }
})