vue 自定義組件實(shí)現(xiàn)通訊錄功能
在線demo:http://tangyupeng.top/dist/index.html#/phone


<template>
<div>
<my-header custom-title="通訊錄" custom-fixed >
<button @touchstart="backBtn" slot="left">首頁(yè)</button>
<button @touchstart="homeBtn" slot="right">+</button>
</my-header>
<my-list :user-data="userData"></my-list> <!-- 傳遞數(shù)據(jù) -->
<my-alert custom-title="呼叫">
<div class="alert_btn">
<button class="aler_tbn" @touchstart="confirmBtn">確認(rèn)</button>
<button class="aler_tbn" @touchstart="cancelBtn">取消</button>
</div>
</my-alert>
</div>
</template>
<script>
import Vue from 'vue';
import Vuex from 'vuex';
var userData=[
{"index":"A","users":[
{"name":"a1","tel":"13333333331"},
{"name":"a2","tel":"13333333332"},
{"name":"a3","tel":"13333333333"},
]},
{"index":"B","users":[
{"name":"b1","tel":"13333333331"},
{"name":"b2","tel":"13333333332"},
{"name":"b3","tel":"13333333333"},
]},
{"index":"C","users":[
{"name":"c1","tel":"13333333331"},
{"name":"c2","tel":"13333333332"},
{"name":"c3","tel":"13333333333"},
]},
{"index":"D","users":[
{"name":"d1","tel":"13333333331"},
{"name":"d2","tel":"13333333332"},
{"name":"d3","tel":"13333333333"},
]},
{"index":"E","users":[
{"name":"e1","tel":"13333333331"},
{"name":"e2","tel":"13333333332"},
{"name":"e3","tel":"13333333333"},
]},
{"index":"F","users":[
{"name":"f1","tel":"13333333331"},
{"name":"f2","tel":"13333333332"},
{"name":"f3","tel":"13333333333"},
]},
{"index":"F1","users":[
{"name":"f1","tel":"13333333331"},
{"name":"f2","tel":"13333333332"},
{"name":"f3","tel":"13333333333"},
]},
{"index":"F2","users":[
{"name":"f1","tel":"13333333331"},
{"name":"f2","tel":"13333333332"},
{"name":"f3","tel":"13333333333"},
]},
{"index":"F3","users":[
{"name":"f1","tel":"13333333331"},
{"name":"f2","tel":"13333333332"},
{"name":"f3","tel":"13333333333"},
]},
{"index":"F4","users":[
{"name":"f1","tel":"13333333331"},
{"name":"f2","tel":"13333333332"},
{"name":"f3","tel":"13333333333"},
]},
{"index":"F5","users":[
{"name":"f1","tel":"13333333331"},
{"name":"f2","tel":"13333333332"},
{"name":"f3","tel":"13333333333"},
]},
];
var busVm=new Vue(); //空實(shí)例 非父子傳遞值
Vue.component('my-header',{
template:`<div id="header" :style="{'position':customFixed ? 'fixed':'static'}">
<slot name="left"></slot>
{{customTitle}}
<slot name="right"></slot>
</div>`,
props:{
'customTitle':{
type:String,
default:"標(biāo)題"
},
'customFixed':{
type:Boolean,
default:false
}
}
})
Vue.component('my-alert',{
template:`<div id="alert" ref="alert">
<div class="alert_content">
<div class="alert_title">
{{customTitle}}
</div>
<div class="alert_body">{{customBody}}</div>
<slot></slot>
</div>
</div>`,
props:{
'customTitle':{
type:String,
default:"彈窗"
},
},
data:function(){
return{
'customBody':''
}
},
mounted:function(){
busVm.$on('changeEvents',function(ev){
console.log(ev);
this.customBody=ev;
this.$refs.alert.style.display="flex"
}.bind(this));
}
})
Vue.component('my-list',{
template:`<div id="list">
<ul class="list_user" ref="listuser" @touchmove="bMove=true">
<li v-for="item in userData">
<p>{{item.index}}</p>
<ul>
<li @touchend="showtel(user.tel)" v-for="user in item.users">{{user.name}}</li>
</ul>
</li>
</ul>
<ul class="list_index" ref="listIndex">
<li @touchstart="setScroll" v-for="item in userIndex">{{item}}</li>
</ul>
</div>`,
props:{
'user-data':{
type:Array,
default:function(){
return [];
}
}
},
data:function(){
return {
bMove:false
}
},
computed:{
userIndex:function(){
console.log(this.userData)
console.log(this.filterIndex(this.userData))
return this.filterIndex(this.userData);
}
},
methods:{
filterIndex:function(data){
var result=[];
for(var i=0; i<data.length;i++){
if(data[i].index){
result.push(data[i].index);
}
}
return result;
},
setlistIndexPos:function(){
// 1、ref 加在普通的元素上,用this.ref.name 獲取到的是dom元素
// 2、ref 加在子組件上,用this.ref.name 獲取到的是組件實(shí)例,可以使用組件的所有方法。
// 3、如何利用 v-for 和 ref 獲取一組數(shù)組或者dom 節(jié)點(diǎn)
var iH= this.$refs.listIndex.offsetHeight;
this.$refs.listIndex.style.marginTop=-iH/2 +'px';
},
showtel:function(ev){
if(!this.bMove){
busVm.$emit("changeEvents",ev)
}else{
this.bMove=false;
}
},
setScroll:function(ev){
console.log(ev.target.innerHTML);
var ap=this.$refs.listuser.getElementsByTagName('p');
for(var i=0;i<ap.length;i++){
if(ap[i].innerHTML==ev.target.innerHTML){
document.body.scrollTop=ap[i].offsetTop;
document.documentElement.scrollTop=ap[i].offsetTop;
window.scrollTop=ap[i].offsetTop;
console.log(ap[i].offsetTop);
}
}
}
},
mounted:function(){
this.setlistIndexPos();
}
})
export default {
name: "HelloWorld",
data() {
return {
userData:userData //掛載數(shù)據(jù)
}
},
methods:{
backBtn:function(){
alert("123")
},
homeBtn:function(){
alert("123")
},
confirmBtn:function(){
alert("a");
},
cancelBtn:function(){
console.log(this);
this.$children[2].$el.style.display="none"; //此處需要從外級(jí)找到
}
},
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
.page-container {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
#alert{width: 100%; height: 100%; background: rgba(0,0,0,0.5); position: fixed; top: 0; top: 0; z-index: 20px; display: none;}
#alert .alert_content{width: 200px;position: relative; height: 150px; background: #fff;border-radius: 10px; margin: auto;}
.alert_body{height: 50px; line-height: 50px; text-align: center;}
.alert_btn{position: absolute;right: 0 ;bottom:0;}
.list_index{ position: fixed;list-style: none; padding-right: 10px; font-size: 20px; font-weight: 700;
top: 50%;
right: 0;}
.alert_title{padding-top: 20px;}
#list .list_user p{background: #ccc; padding-left: 10px}
#list .list_user ul li{ padding-left: 10px;border-bottom: 1px solid #ccc; line-height:30px;}
#list{
width: 100%;
text-align: left;
float: left;
position: relative; top: 40px; overflow: hidden;
}
.aler_tbn{padding: 5px; margin-bottom: 5px; border-radius: 5px;}
button{background: #f60; color: #fff;}
#header{width: 100%;height:40px; background: #666; z-index:9999;color: #fff;text-align: center;line-height: 40px;font-size: 20px;}
#header button{height: 100%;padding: 0 5px}
#header button:nth-of-type(1){float: left}
#header button:nth-of-type(2){float: right}
</style>
總結(jié)
以上所述是小編給大家介紹的vue 通訊錄 自定義組件功能的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue使用自定義指令實(shí)現(xiàn)一鍵復(fù)制功能
在Vue中,通過(guò)自定義指令v-copy和document.execCommand方法,可以實(shí)現(xiàn)點(diǎn)擊按鈕復(fù)制內(nèi)容到剪貼板的功能,適用于處理長(zhǎng)文本或多行文本的復(fù)制需求,而readonly屬性可避免內(nèi)容被修改和移動(dòng)設(shè)備上的虛擬鍵盤(pán)干擾,感興趣的朋友一起看看吧2024-09-09
解決v-for中使用v-if或者v-bind:class失效的問(wèn)題
今天小編就為大家分享一篇解決v-for中使用v-if或者v-bind:class失效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue-router 控制路由權(quán)限的實(shí)現(xiàn)
這篇文章主要介紹了vue-router 控制路由權(quán)限的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Vue實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求攔截
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求攔截,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
element table列表根據(jù)數(shù)據(jù)設(shè)置背景色
在使用elementui中的el-table時(shí),需要將表的背景色和字體顏色設(shè)置為新顏色,本文就來(lái)介紹一下element table列表根據(jù)數(shù)據(jù)設(shè)置背景色,感興趣的可以了解一下2023-08-08
vue之webpack -v報(bào)錯(cuò)解決方案總結(jié)
這篇文章主要介紹了vue之webpack -v報(bào)錯(cuò)解決方案總結(jié),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
淺談Vue頁(yè)面級(jí)緩存解決方案feb-alive (下)
這篇文章主要介紹了淺談Vue頁(yè)面級(jí)緩存解決方案feb-alive(下),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

