介紹
通過 Contact 組件可以實(shí)現(xiàn)聯(lián)系人的展示、選擇、編輯等功能。
引入
import Vue from 'vue';
import { ContactCard, ContactList, ContactEdit } from 'vant';
Vue.use(ContactCard);
Vue.use(ContactList);
Vue.use(ContactEdit);
代碼演示
基礎(chǔ)用法
<!-- 聯(lián)系人卡片 -->
<van-contact-card
:type="cardType"
:name="currentContact.name"
:tel="currentContact.tel"
@click="showList = true"
/>
<!-- 聯(lián)系人列表 -->
<van-popup v-model="showList" position="bottom">
<van-contact-list
v-model="chosenContactId"
:list="list"
@add="onAdd"
@edit="onEdit"
@select="onSelect"
/>
</van-popup>
<!-- 聯(lián)系人編輯 -->
<van-popup v-model="showEdit" position="bottom">
<van-contact-edit
:contact-info="editingContact"
:is-edit="isEdit"
@save="onSave"
@delete="onDelete"
/>
</van-popup>
export default {
data() {
return {
chosenContactId: null,
editingContact: {},
showList: false,
showEdit: false,
isEdit: false,
list: [{
name: '張三',
tel: '13000000000',
id: 0
}]
};
},
computed: {
cardType() {
return this.chosenContactId !== null ? 'edit' : 'add';
},
currentContact() {
const id = this.chosenContactId;
return id !== null ? this.list.filter(item => item.id === id)[0] : {};
}
},
methods: {
// 添加聯(lián)系人
onAdd() {
this.editingContact = { id: this.list.length };
this.isEdit = false;
this.showEdit = true;
},
// 編輯聯(lián)系人
onEdit(item) {
this.isEdit = true;
this.showEdit = true;
this.editingContact = item;
},
// 選中聯(lián)系人
onSelect() {
this.showList = false;
},
// 保存聯(lián)系人
onSave(info) {
this.showEdit = false;
this.showList = false;
if (this.isEdit) {
this.list = this.list.map(item => item.id === info.id ? info : item);
} else {
this.list.push(info);
}
this.chosenContactId = info.id;
},
// 刪除聯(lián)系人
onDelete(info) {
this.showEdit = false;
this.list = this.list.filter(item => item.id !== info.id);
if (this.chosenContactId === info.id) {
this.chosenContactId = null;
}
}
}
};
不可編輯
<van-contact-card
type="edit"
name="張三"
tel="13000000000"
:editable="false"
/>
API
ContactCard Props
參數(shù) | 說明 | 類型 | 默認(rèn)值 |
---|
type | 類型,可選值為 add edit | string | add |
name | 聯(lián)系人姓名 | string | - |
tel | 聯(lián)系人手機(jī)號(hào) | string | - |
add-text | 添加時(shí)的文案提示 | string | 添加訂單聯(lián)系人信息 |
ContactCard Events
事件名 | 說明 | 回調(diào)參數(shù) |
---|
click | 點(diǎn)擊時(shí)觸發(fā) | event: Event |
ContactList Props
參數(shù) | 說明 | 類型 | 默認(rèn)值 |
---|
v-model | 當(dāng)前選中聯(lián)系人的 id | number | string | - |
list | 聯(lián)系人列表 | Contact[] | [] |
add-text | 新建按鈕文案 | string | 新建聯(lián)系人 |
default-tag-text v2.3.0 | 默認(rèn)聯(lián)系人標(biāo)簽文案 | string | - |
ContactList Events
事件名 | 說明 | 回調(diào)參數(shù) |
---|
add | 點(diǎn)擊新增按鈕時(shí)觸發(fā) | - |
edit | 點(diǎn)擊編輯按鈕時(shí)觸發(fā) | item: 當(dāng)前聯(lián)系人對(duì)象,index: 索引 |
select | 切換選中的聯(lián)系人時(shí)觸發(fā) | item: 當(dāng)前聯(lián)系人對(duì)象,index: 索引 |
ContactEdit Props
參數(shù) | 說明 | 類型 | 默認(rèn)值 |
---|
contact-info | 聯(lián)系人信息 | object | [] |
is-edit | 是否為編輯聯(lián)系人 | boolean | false |
is-saving | 是否顯示保存按鈕加載動(dòng)畫 | boolean | false |
is-deleting | 是否顯示刪除按鈕加載動(dòng)畫 | boolean | false |
tel-validator | 手機(jī)號(hào)格式校驗(yàn)函數(shù) | (tel: string) => boolean | - |
show-set-default v2.3.0 | 是否顯示默認(rèn)聯(lián)系人欄 | boolean | false |
set-default-label v2.3.0 | 默認(rèn)聯(lián)系人欄文案 | string | - |
ContactEdit Events
事件名 | 說明 | 回調(diào)參數(shù) |
---|
save | 點(diǎn)擊保存按鈕時(shí)觸發(fā) | content:表單內(nèi)容 |
delete | 點(diǎn)擊刪除按鈕時(shí)觸發(fā) | content:表單內(nèi)容 |
Contact 數(shù)據(jù)結(jié)構(gòu)
鍵名 | 說明 | 類型 |
---|
id | 每位聯(lián)系人的唯一標(biāo)識(shí) | number | string |
name | 聯(lián)系人姓名 | string |
tel | 聯(lián)系人手機(jī)號(hào) | number | string |
isDefault | 是否為默認(rèn)聯(lián)系人 | boolean |
更多建議: