Vue自嵌套樹組件使用方法詳解
更新時間:2021年08月17日 09:26:42 作者:xiaolidan00
這篇文章主要為大家詳細(xì)介紹了Vue自嵌套樹組件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Vue自嵌套樹組件的使用方法,供大家參考,具體內(nèi)容如下
效果圖

注意事項
- 組件自嵌套,定義名稱時就定義為組件名
- 單選和多選用戶時,以最頂級父組件的屬性為準(zhǔn),由于組件內(nèi)不能同步修改prop,故采用data注冊另一個同類型數(shù)值用于接收組件內(nèi)改變,并使用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: {//當(dāng)前用戶樹父級id
type: Number,
default: 0
},
selectUser: {//當(dāng)前選中的用戶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 () {//單選用戶,當(dāng)前級同步到父級
if (this.selectUser1 != this.selectUser) {
this.$emit("update:select-user", this.selectUser1);
}
},
selectUser () {//單選用戶,當(dāng)前級同步于父級
if (this.selectUser1 != this.selectUser) {
this.selectUser1 = this.selectUser;
}
},
selectUsers1 () {//多選用戶,當(dāng)前級同步到父級
if (this.selectUsers1 != this.selectUsers) {
this.$emit("update:select-users", this.selectUsers1);
}
},
selectUsers () {//多選用戶,當(dāng)前級同步于父級
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) {//當(dāng)前父級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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決vue語法會有延遲加載顯現(xiàn){{xxx}}的問題
今天小編就為大家分享一篇解決vue語法會有延遲加載顯現(xiàn){{xxx}}的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue?draggable組件實現(xiàn)拖拽及點擊無效問題的解決
這篇文章主要介紹了vue?draggable組件實現(xiàn)拖拽及點擊無效問題的解決,只需要在設(shè)置handle屬性就可以了,.defaultTypeTag 是要拖拽的塊的類名,要注意的是需要做點擊事件的項不能包含在這個類名里面,不然會無法觸發(fā)點擊事件,詳細(xì)解決辦法跟隨小編一起學(xué)習(xí)吧2022-05-05
vue實現(xiàn)加載頁面自動觸發(fā)函數(shù)(及異步獲取數(shù)據(jù))
這篇文章主要介紹了vue實現(xiàn)加載頁面自動觸發(fā)函數(shù)(及異步獲取數(shù)據(jù)),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

