最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue遞歸組件實(shí)戰(zhàn)之簡(jiǎn)單樹形控件實(shí)例代碼

 更新時(shí)間:2019年08月27日 15:33:42   作者:heath_learning  
這篇文章主要介紹了vue遞歸組件實(shí)戰(zhàn)之簡(jiǎn)單樹形控件的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1、遞歸組件-簡(jiǎn)單樹形控件預(yù)覽及問(wèn)題

 

在編寫樹形組件時(shí)遇到的問(wèn)題:

  • 組件如何才能遞歸調(diào)用?
  • 遞歸組件點(diǎn)擊事件如何傳遞?

2、樹形控件基本結(jié)構(gòu)及樣式

<template>
 <ul class="vue-tree">
  <li class="tree-item">
   <div class="tree-content"><!--節(jié)點(diǎn)內(nèi)容-->
    <div class="expand-arrow"></div><!--展開或收縮節(jié)點(diǎn)按鈕-->
    <div class="tree-label">小學(xué)</div><!--節(jié)點(diǎn)文本內(nèi)容-->
   </div>
   <ul class="sub-tree"><!--子節(jié)點(diǎn)-->
    <li class="tree-item expand">
     <div class="tree-content">
      <div class="expand-arrow"></div>
      <div class="tree-label">語(yǔ)文</div>
     </div>
    </li>
    <li class="tree-item">
     <div class="tree-content">
      <div class="expand-arrow"></div>
      <div class="tree-label">數(shù)學(xué)</div>
     </div>
    </li>
   </ul>
  </li>
 </ul>
</template>

<style lang="stylus">
.vue-tree{
 list-style: none;
 padding: 0;
 margin: 0;
 .tree-item{
  cursor: pointer;
  transition: background-color .2s;
  .tree-content{
   position: relative;
   padding-left: 28px;
   &:hover{
    background-color: #f0f7ff;
   }
  }
  .expand-arrow{
   position: absolute;
   top: 0;
   left: 0;
   width: 28px;
   height: 28px;
   cursor: pointer;
   &::after{
    position: absolute;
    top: 50%;
    left: 50%;
    display: block;
    content: ' ';
    border-width: 5px;
    border-style: solid;
    border-color: transparent;
    border-left-color: #ccc;
    margin: -5px 0 0 -2.5px;
    transition: all .2s;
   }
  }
  &.expand{
   &>.tree-content{
    background-color: #f0f7ff;
    &>.expand-arrow{
     &::after{
      transform: rotate(90deg);
      margin: -2.5px 0 0 -5px;
     }
    }
   }
  }
  .tree-label{
   height: 28px;
   line-height: 28px;
   font-size: 14px;
  }
  .sub-tree{
   display: none;
   list-style: none;
   padding: 0 0 0 28px;
   margin: 0;
  }
  &.expand>.sub-tree{
   display: block;
  }
  &.no-child{
   &>.tree-content{
    &>.expand-arrow{
     display: none;
    }
   }
  }
 }
}
</style>

3、組件目錄及數(shù)據(jù)結(jié)構(gòu)

目錄結(jié)構(gòu)

vue-tree

VueTree.vue
TreeItem.vue

樹形控件數(shù)據(jù)結(jié)構(gòu)

let treeData = [
 {
  text: "一級(jí)", // 顯示的文字
  expand: false, // 默認(rèn)是否展開
  children: [ // 子節(jié)點(diǎn)
   {
    text: "一級(jí)-1",
    expand: false,
   },
   {
    text: "一級(jí)-2",
    expand: false,
    children: [
     {
      text: "一級(jí)-2-1",
      expand: false,
     },
     {
      text: "一級(jí)-2-2",
      expand: false,
     }
    ]
   }
  ]
 }
];

3.1、 TreeItem.vue 代碼

<template>
 <li class="tree-item" :class="{expand: isExpand, 'no-child': !treeItemData.children || treeItemData.children.length === 0}">
  <div class="tree-content" @click="_clickEvent">
   <div class="expand-arrow" @click.stop="expandTree()"></div>
   <div class="tree-label">{{treeItemData.text}}</div>
  </div>
  <ul class="sub-tree" v-if="treeItemData.children && treeItemData.children.length > 0">
   <!--TreeItem組件中調(diào)用TreeItem組件-->
   <TreeItem
    v-for="item in treeItemData.children"
    :tree-item-data="item"
    :key="uuid()"
    :tree-click-event="treeClickEvent"></TreeItem>
  </ul>
 </li>
</template>

<script>
 export default {
  name: "TreeItem",
  props: {
   treeItemData: {
    type: Object,
    default(){
     return {};
    }
   },
   // 節(jié)點(diǎn)點(diǎn)擊事件
   treeClickEvent: {
    type: Function,
    default() {
     return function () {};
    }
   }
  },
  data(){
   return {
    // 節(jié)點(diǎn)是否展開
    isExpand: this.treeItemData.expand || false
   }
  },
  methods: {
   // 展開/收縮
   expandTree(flag){
    if(!this.treeItemData.children || this.treeItemData.children.length === 0){
     return;
    }
    if(typeof flag === 'undefined'){
     flag = !this.isExpand;
    }else{

     flag = !!flag;
    }
    this.isExpand = flag;
   },
   // 創(chuàng)建一個(gè)唯一id
   uuid(){
    let str = Math.random().toString(32);
    str = str.substr(2);
    return str;
   },
   // 節(jié)點(diǎn)點(diǎn)擊事件
   _clickEvent(){
    // 如果有傳遞事件函數(shù),則調(diào)用事件函數(shù)并傳遞當(dāng)前節(jié)點(diǎn)數(shù)據(jù)及組件
    if(this.treeClickEvent && typeof this.treeClickEvent === 'function'){
     this.treeClickEvent(this.treeItemData, this);
    }
   }
  }
 }
</script>

3.1.1、解決 組件如何才能遞歸調(diào)用? 問(wèn)題

在組件模板內(nèi)調(diào)用自身 必須明確定義組件的name屬性 ,并且遞歸調(diào)用時(shí)組件名稱就是name屬性。如在 TreeItem.vue 組件中組件的name名稱為'TreeItem',那么在template中調(diào)用時(shí)組件名稱就必須是 <TreeItem> 。

當(dāng)然也可以全局注冊(cè)組件,具體可以查看vue官方文檔 遞歸組件

3.1.2、解決 遞歸組件點(diǎn)擊事件如何傳遞? 問(wèn)題

我這里的解決方案是使用 props 將事件函數(shù)傳遞進(jìn)來(lái),在點(diǎn)擊節(jié)點(diǎn)的時(shí)候調(diào)用事件函數(shù),并把相應(yīng)的數(shù)據(jù)傳遞進(jìn)去。

之前也嘗試過(guò)使用 $emit 的形式并把數(shù)據(jù)傳遞過(guò)去,由于是遞歸組件,這樣一直 $emit ,到最外層時(shí)傳遞的數(shù)據(jù)就變了,比如傳遞是第3層節(jié)點(diǎn)的數(shù)據(jù),到最后執(zhí)行時(shí)數(shù)據(jù)就變成第1層節(jié)點(diǎn)的數(shù)據(jù)了

4、 VueTree.vue 組件

<template>
 <ul class="vue-tree">
  <TreeItem
    v-for="(item, index) in treeData"
    :key="index"
    :treeItemData="item"
    :tree-click-event="treeClickEvent"></TreeItem>
 </ul>
</template>

<script>
 import TreeItem from "./TreeItem";
 export default {
  name: "VueTreeMenu",
  components: {
   TreeItem
  },
  props: {
   // 樹形控件數(shù)據(jù)
   treeData: {
    type: Array,
    default(){
     return [];
    }
   },
   // 節(jié)點(diǎn)點(diǎn)擊事件
   treeClickEvent: {
    type: Function,
    default() {
     return function () {};
    }
   }
  }
 }
</script>

<style lang="stylus">
.vue-tree{
 list-style: none;
 padding: 0;
 margin: 0;
 .tree-item{
  cursor: pointer;
  transition: background-color .2s;
  .tree-content{
   position: relative;
   padding-left: 28px;
   &:hover{
    background-color: #f0f7ff;
   }
  }
  .expand-arrow{
   position: absolute;
   top: 0;
   left: 0;
   width: 28px;
   height: 28px;
   cursor: pointer;
   &::after{
    position: absolute;
    top: 50%;
    left: 50%;
    display: block;
    content: ' ';
    border-width: 5px;
    border-style: solid;
    border-color: transparent;
    border-left-color: #ccc;
    margin: -5px 0 0 -2.5px;
    transition: all .2s;
   }
  }
  &.expand{
   &>.tree-content{
    background-color: #f0f7ff;
    &>.expand-arrow{
     &::after{
      transform: rotate(90deg);
      margin: -2.5px 0 0 -5px;
     }
    }
   }
  }
  .tree-label{
   height: 28px;
   line-height: 28px;
   font-size: 14px;
  }
  .sub-tree{
   display: none;
   list-style: none;
   padding: 0 0 0 28px;
   margin: 0;
  }
  &.expand>.sub-tree{
   display: block;
  }
  &.no-child{
   &>.tree-content{
    /*padding-left: 0;*/
    &>.expand-arrow{
     display: none;
    }
   }
  }
 }
}
</style>

5、使用樹形組件

<template>
 <div class="app" id="app">
  <VueTree :tree-data="treeData2" :tree-click-event="treeClickEvent"></VueTree>
 </div>
</template>

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

export default {
 name: 'app',
 data(){
  return {
   treeData2: [
    {
     text: "一級(jí)", // 顯示的文字
     expand: false, // 默認(rèn)是否展開
     children: [
      {
       text: "二級(jí)-1",
       expand: false,
      },
      {
       text: "二級(jí)-2",
       expand: false,
       children: [
        {
         text: "三級(jí)-1",
         expand: false,
        },
        {
         text: "三級(jí)-2",
         expand: false,
         children: [
          {
           text: "四級(jí)-1",
           expand: false,
          }
         ]
        }
       ]
      }
     ]
    },
    {
     text: "一級(jí)-2",
     expand: false
    }
   ]
  }
 },
 methods: {
  treeClickEvent(item, treeItem){
   console.log(item);
  }
 },
 components: {
  VueTree
 }
}
</script>

總結(jié)

以上所述是小編給大家介紹的vue遞歸組件實(shí)戰(zhàn)之簡(jiǎn)單樹形控件實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

  • 基于Vue3實(shí)現(xiàn)視頻播放與截圖功能

    基于Vue3實(shí)現(xiàn)視頻播放與截圖功能

    這篇文章主要為大家詳細(xì)介紹了如何通過(guò)Vue3實(shí)現(xiàn)簡(jiǎn)單的視頻播放與截圖功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-04-04
  • vue3?pinia實(shí)現(xiàn)持久化詳解

    vue3?pinia實(shí)現(xiàn)持久化詳解

    Pinia是Vue3的狀態(tài)管理工具,安裝后在入口文件引入,定義store并在組件中使用,本文主要介紹了Vue3中如何使用pinia實(shí)現(xiàn)持久化,需要的可以參考下
    2024-11-11
  • 基于vue的video播放器的實(shí)現(xiàn)示例

    基于vue的video播放器的實(shí)現(xiàn)示例

    這篇文章主要介紹了基于vue的video播放器的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Vue3+Vite實(shí)現(xiàn)一個(gè)Markdown編輯器組件

    Vue3+Vite實(shí)現(xiàn)一個(gè)Markdown編輯器組件

    在現(xiàn)代前端開發(fā)中,Markdown 編輯器廣泛應(yīng)用于博客,文檔,Wiki,代碼注釋等場(chǎng)景,本文將使用 Vue 3 構(gòu)建一個(gè)簡(jiǎn)單的 Markdown 編輯器組件,感興趣的小伙伴可以了解下
    2025-04-04
  • React和Vue實(shí)現(xiàn)路由懶加載的示例代碼

    React和Vue實(shí)現(xiàn)路由懶加載的示例代碼

    路由懶加載是一項(xiàng)關(guān)鍵技術(shù),它可以幫助我們提高Web應(yīng)用的加載速度,本文主要介紹了React和Vue實(shí)現(xiàn)路由懶加載的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • Vue3?源碼分析reactive?readonly實(shí)例

    Vue3?源碼分析reactive?readonly實(shí)例

    這篇文章主要為大家介紹了Vue3?源碼分析reactive?readonly實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • vue elementUI下拉框值無(wú)法選中問(wèn)題及解決方案

    vue elementUI下拉框值無(wú)法選中問(wèn)題及解決方案

    大家在寫系統(tǒng)的時(shí)候,會(huì)有這樣的需求:點(diǎn)擊修改后把需要修改的數(shù)據(jù)放入表單,其中會(huì)有下拉單選框、下拉多選框,展示下拉框單選框內(nèi)的數(shù)據(jù)只需要將所選id賦值給下拉框綁定的值就可以了,下面給大家分享vue elementUI下拉框值無(wú)法選中問(wèn)題,感興趣的朋友一起看看吧
    2024-03-03
  • Vue項(xiàng)目中禁用ESLint的幾種常見(jiàn)方法小結(jié)

    Vue項(xiàng)目中禁用ESLint的幾種常見(jiàn)方法小結(jié)

    Vue ESLint是一個(gè)基于ESLint的插件,它專門為Vue.js應(yīng)用設(shè)計(jì),用于提供JavaScript代碼風(fēng)格檢查和最佳實(shí)踐規(guī)則,Vue項(xiàng)目通常會(huì)集成ESLint,目的是為了提升代碼質(zhì)量、保持一致性和可維護(hù)性,本文介紹了Vue項(xiàng)目中禁用ESLint的幾種常見(jiàn)方法,需要的朋友可以參考下
    2024-07-07
  • 解析Vue.use()是干什么的

    解析Vue.use()是干什么的

    今天通過(guò)本文給大家分享Vue.use是什么,主要包括vueEsign?插件的install是什么,element-ui的install是什么,為什么有的庫(kù)就不需要使用Vue.use,對(duì)vue.use()相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-06-06
  • vue項(xiàng)目首屏加載時(shí)間優(yōu)化實(shí)戰(zhàn)

    vue項(xiàng)目首屏加載時(shí)間優(yōu)化實(shí)戰(zhàn)

    單頁(yè)面應(yīng)用的一個(gè)問(wèn)題就是首頁(yè)加載東西過(guò)多,加載時(shí)間過(guò)長(zhǎng)。特別在移動(dòng)端,單頁(yè)面應(yīng)用的首屏加載優(yōu)化更是繞不開的話題,這篇文章主要介紹了vue項(xiàng)目首屏加載時(shí)間優(yōu)化實(shí)戰(zhàn),感興趣的小伙伴們可以參考一下
    2019-04-04

最新評(píng)論

阿拉善盟| 新巴尔虎左旗| 巴林右旗| 马鞍山市| 阿坝县| 宁阳县| 平南县| 扎囊县| 鞍山市| 台湾省| 溧阳市| 陈巴尔虎旗| 建水县| 绥棱县| 上饶县| 汕头市| 江孜县| 雷波县| 平罗县| 辽源市| 太仓市| 彰化市| 英超| 五台县| 新野县| 忻城县| 陆良县| 当雄县| 大渡口区| 鲜城| 平湖市| 牡丹江市| 古田县| 黄浦区| 丹巴县| 庐江县| 海城市| 重庆市| 阳原县| 大荔县| 德庆县|