欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue自嵌套樹組件使用方法詳解

 更新時間:2021年08月17日 09:26:42   作者:xiaolidan00  
這篇文章主要為大家詳細介紹了Vue自嵌套樹組件的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Vue自嵌套樹組件的使用方法,供大家參考,具體內容如下

效果圖


注意事項

  • 組件自嵌套,定義名稱時就定義為組件名
  • 單選和多選用戶時,以最頂級父組件的屬性為準,由于組件內不能同步修改prop,故采用data注冊另一個同類型數(shù)值用于接收組件內改變,并使用update,同步更新到prop上
  • 展開組件才開始加載用戶列表
<template>
  <ul v-show="isShow" ref="user-tree">
    <li v-for="(item, idx) in userList" :key="idx">
      <div>
        <!-- 多選用戶樹 -->
        <input
          class="primary"
          type="checkbox"
          v-model="selectUsers1"
          :value="item.userId"
          v-show="isCheck"
        />
        <!-- 單選用戶樹 -->
        <span
          @click="onSelect(item)"
          :style="{
            color: selectUser1 == item.userId ? 'red' : '',
            cursor: 'pointer',
          }"
        >
          <i class="iconfont icon-user"></i> {{ item.userName }}</span
        >
        <!-- 展開用戶樹 -->
        <i
          class="iconfont icon-right"
          v-if="item.haveChild"
          @click="expandItem(idx)"
        ></i>
      </div>
      <!-- 嵌套用戶樹 -->
      <user-tree
        :user-id="item.userId"
        v-if="item.haveChild"
        :is-show.sync="item.isShow"
        :select-user.sync="selectUser1"
        :select-users.sync="selectUsers1"
        :is-check="isCheck"
      ></user-tree>
    </li>
  </ul>
</template>

<script> 
export default {
  name: "user-tree",//定義為組件名,否則自嵌套時報未注冊自身組件錯誤
  props: {
    isShow: {//是否展開用戶列表
      type: Boolean,
      default: false
    },
    userId: {//當前用戶樹父級id
      type: Number,
      default: 0
    },
    selectUser: {//當前選中的用戶id
      type: Number,
      default: 0
    },
    selectUsers: {//多選用戶
      type: Array,
      default: function () {
        return [];
      }
    },
    isCheck: {//是否多選功能
      type: Boolean,
      default: false
    }
  },

  data: () => ({
    userList: [], //用戶列表
    selectUser1: 1,//單選用戶
    selectUsers1: [],//多選用戶
    isLoad: true
  }),
  watch: {
    selectUser1 () {//單選用戶,當前級同步到父級
      if (this.selectUser1 != this.selectUser) {
        this.$emit("update:select-user", this.selectUser1);
      }
    },
    selectUser () {//單選用戶,當前級同步于父級
      if (this.selectUser1 != this.selectUser) {
        this.selectUser1 = this.selectUser;
      }
    },
    selectUsers1 () {//多選用戶,當前級同步到父級
      if (this.selectUsers1 != this.selectUsers) {
        this.$emit("update:select-users", this.selectUsers1);

      }
    },
    selectUsers () {//多選用戶,當前級同步于父級
      if (this.selectUsers1 != this.selectUsers) {
        this.selectUsers1 = this.selectUsers;
      }
    },
    isShow () {
      if (this.isShow) {
        this.getUserList();
      }
    }
  },
  methods: {
    onSelect (item) {//單選用戶
      this.$emit("update:select-user", item.userId);

    },

    expandItem (idx) {//展開用戶樹
      if (this.userList[idx].isShow) {
        this.userList[idx].isShow = false;
      } else {
        this.userList[idx].isShow = true;
      }

    },
    getUserList () {
      var list = [];
      for (var i = 0; i < 10; i++) {
        var userId = Math.round(Math.random() * 10000);
        list.push({
          userId: userId,
          userName: "user-" + userId,
          haveChild: i % 2,//是否有子樹
          isShow: false //是否展示子樹
        });
      }
      this.userList = list;


    },

  },
  mounted () {

    if (this.userId == 1) {//當前父級userId為根用戶id,就加載并展開用戶樹
      this.getUserList(this.userId);
    }
  }
};
</script> 

使用樹組件

<div>{{ selectUser }}</div>
    <div>
      <span v-for="item in selectUsers" :key="item">【{{ item }}】</span>
    </div>
    <user-tree
      :user-id="1"
      :is-show="true"
      :select-user.sync="selectUser"
      :select-users.sync="selectUsers"
      :is-check="true"
></user-tree>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論