中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

vue如何使用遞歸組件實現一個樹形控件

發布時間:2022-11-22 09:49:52 來源:億速云 閱讀:144 作者:iii 欄目:開發技術

這篇文章主要介紹“vue如何使用遞歸組件實現一個樹形控件”,在日常操作中,相信很多人在vue如何使用遞歸組件實現一個樹形控件問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”vue如何使用遞歸組件實現一個樹形控件”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

vue版的樹形控件

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>樹形結構2</title>
</head>
<body>
  <div id = "app">
    <tree :folder = "trees" :select = "select"></tree>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    Vue.component('tree', {
      name:"tree",
      template:`<ul>
            <li v-for = "item in folder">
              <span @click = "select(item)">{{ item.label }}</span>
              <tree v-if = "item.children" :folder = "item.children" :select = "select"></tree>
            </li>
          </ul>`,
      props:["folder","select"],
    })
    // <tree v-if = "item.children" :folder = "item.children"></tree>
    var app = new Vue({
      el:"#app",
      data:{
        msg:"hello world",
        trees: [
          {
            id:1,
            label:"1級目錄1",
            show:false,
            children:[
              {
                id:"1-1",
                label:"1.1目錄"
              },
              {
                id:"1-2",
                label:"1.2目錄"
              },
              {
                id:"1-3",
                label:"1.3目錄"
              },
            ]
          },
          {
            id:2,
            label:"1級目錄2",
            show:false
          },
          {
            id:3,
            label:"1級目錄3",
            show:false,
            children:[
              {
                id:"3-1",
                label:"3.1目錄"
              },
              {
                id:"3-2",
                label:"3.2目錄",
                show:false,
                children:[
                  {
                    id:"3-2-1",
                    label:"3.2.1目錄"
                  },
                  {
                    id:"3-2-2",
                    label:"3.2.2目錄"
                  },
                  {
                    id:"3-2-3",
                    label:"3.2.3目錄"
                  }
                ]
              }
            ]
          },
          {
            id:4,
            label:"1級目錄4",
            show:false,
            children:[
              {
                id:"4-1",
                label:"4.1目錄"
              },
              {
                id:"4-2",
                label:"4.2目錄",
                show:false,
                children:[
                  {
                    id:"4-2-1",
                    label:"4.2.1目錄"
                  }
                ]
              }
            ]
          },
          {
            id:5,
            label:"1級目錄5",
            show:false,
            children:[
              {
                id:"5-1",
                label:"5.1目錄",
                show:false,
                children:[
                  {
                    id:"5-1-1",
                    label:"5.1.1目錄"
                  },
                  {
                    id:"5-1-2",
                    label:"5.1.2目錄",
                    show:false,
                    children:[
                      {
                        id:"5-1-2-1",
                        label:"5.1.2.1目錄"
                      },
                    ]
                  }
                ]
              },
              {
                id:"5-2",
                label:"5.2目錄",
                show:false
              }
            ]
          },
        ]
      },
      methods:{
        clickHandler(){
          console.log(23333);
        },
        select(data){
          console.log(data);
        }
      },
      mounted(){
        console.log(this.trees);
      }
    })
  </script>
</body>
</html>

當然我用的全局組件,如果用vue-cli搭建的環境是一樣的,引入組件就可以了,但是一定要注意,組件內必須要用name屬性,而且name的名稱要和組件名稱(組件標簽名稱)一致才可以

貼一個項目中用的模板吧,相當于做筆記了

<template>
  <ul class = "dataBaseTree">
    <li v-for = "(item,index) in folder" :key = "index">
      <span @click = "select(item)" :class = "{'active':currentId == item.id}">
        <i class = "folderIcon" v-if = "item.children">
          <icon :icon = "'xiala'" v-if = "item.show"></icon>
          <icon :icon = "'xiala2'" v-else></icon>
        </i>
        {{ item.label }}
      </span>
      <el-collapse-transition>
        <DatabaseTree v-if = "item.children && item.show" :folder = "item.children" :select = "select" :currentId = "currentId"></DatabaseTree>
      </el-collapse-transition>
    </li>
  </ul>
</template>

<script>
  import { mapGetters , mapActions} from 'vuex';
  export default{
    name:"DatabaseTree",
    props:["folder","select","currentId"],
    data(){
      return{
        addParams:{
          label:"",
          children:[]
        },
        noteData:{
          children:[]
        }
      }
    },
     computed:{
      ...mapGetters(["catalog"])
    },
    methods:{}
  }
</script>

<style lang="scss" scoped>
  .dataBaseTree{
    padding-left:12%;
    line-height:40px;
    ul{
      padding-left:12%;
      line-height:40px;
      li{
        span{
          display:inline-block;
          padding-left:23%;
          height:100%;
          width:120%;
          color:#ababab;
          font-size:14px;
          position: relative;
          cursor: pointer;
          &:hover{
            background: #EDF0F5;
          }
          .folderIcon{
            color:#BCBCBC;
            position: absolute;
            top:-1px;
            left:22px;
          }
        }
      }
    }
    li{
      position: relative;
      span{
        display:inline-block;
        padding-left:40px;
        font-size:14px;
        height:100%;
        width:120%;
        cursor: pointer;
        position: relative;
        right:25px;
        top:-2px;
        color:#ababab;
        &:hover{
          background: #EDF0F5;
        }
        .titleIcon{
          color:#C3C3C3;
          font-size:16px;
          position: absolute;
          top:12px;
          left:16px;
        }
        .folderIcon{
          color:#BCBCBC;
          position: absolute;
          top:-1px;
          left:22px;
        }
      }
      .active{
        background: #EDF0F5;
      }
    }
  }
</style>

vue版的就到這里了

 下面貼一個原生js版的,感興趣的小伙伴可以繼續往下看

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    var tree=[
        {
          id:1,
          label:"1級目錄1",
          children:[
            {
              id:"1-1",
              label:"1.1目錄"
            },
            {
              id:"1-2",
              label:"1.2目錄"
            },
            {
              id:"1-3",
              label:"1.3目錄"
            },
          ]
        },
        {
          id:2,
          label:"1級目錄2",
        },
        {
          id:3,
          label:"1級目錄3",
          children:[
            {
              id:"3-1",
              label:"3.1目錄"
            },
            {
              id:"3-2",
              label:"3.2目錄",
              children:[
                {
                  id:"3-2-1",
                  label:"3.2.1目錄"
                },
                {
                  id:"3-2-2",
                  label:"3.2.2目錄"
                },
                {
                  id:"3-2-3",
                  label:"3.2.3目錄"
                }
              ]
            }
          ]
        },
        {
          id:4,
          label:"1級目錄4",
          children:[
            {
              id:"4-1",
              label:"4.1目錄"
            },
            {
              id:"4-2",
              label:"4.2目錄",
              children:[
                {
                  id:"4-2-1",
                  label:"4.2.1目錄"
                }
              ]
            }
          ]
        },
        {
          id:5,
          label:"1級目錄5",
          children:[
            {
              id:"5-1",
              label:"5.1目錄",
              children:[
                {
                  id:"5-1-1",
                  label:"5.1.1目錄"
                },
                {
                  id:"5-1-2",
                  label:"5.1.2目錄",

                  children:[
                    {
                      id:"5-1-2-1",
                      label:"5.1.2.1目錄"
                    },
                  ]
                }
              ]
            },
            {
              id:"5-2",
              label:"5.2目錄"
            }
          ]
        },
      ];
    var render = function(tree) {
      if (!tree) return null
      var ul = document.createElement('ul');
      for(var i = 0; i < tree.length;i++){
        var li = document.createElement('li')
        // 創建span標簽
        var span = document.createElement('span'); span.innerText = tree[i].label;
        li.appendChild(span);
        if(tree[i].children){
          var sub = render(tree[i].children);
          li.appendChild(sub);
        }
        ul.appendChild(li);
      }
      return ul
    };
    document.body.innerHTML = '';
    document.body.appendChild(render(tree)); 
  </script>
</body>
</html>

為什么要使用Vue

Vue是一款友好的、多用途且高性能的JavaScript框架,使用vue可以創建可維護性和可測試性更強的代碼庫,Vue允許可以將一個網頁分割成可復用的組件,每個組件都包含屬于自己的HTML、CSS、JavaScript,以用來渲染網頁中相應的地方,所以越來越多的前端開發者使用vue。

到此,關于“vue如何使用遞歸組件實現一個樹形控件”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

vue
AI

栾城县| 克拉玛依市| 清水河县| 宜州市| 特克斯县| 板桥市| 江津市| 湛江市| 莱芜市| 鞍山市| 惠水县| 休宁县| 嘉义市| 宁德市| 疏附县| 监利县| 喀什市| 资讯| 抚远县| 仁怀市| 抚顺县| 广饶县| 前郭尔| 高邑县| 鲜城| 沧州市| 织金县| 梧州市| 武义县| 潞城市| 眉山市| 钟山县| 达拉特旗| 革吉县| 张家界市| 贵溪市| 庄河市| 达州市| 凉城县| 吴川市| 墨竹工卡县|