這篇文章給大家介紹怎么在vue 中利用自定義組件實(shí)現(xiàn)通訊錄功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

成都創(chuàng)新互聯(lián)長(zhǎng)期為上1000家客戶(hù)提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏(yíng)平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為龍南企業(yè)提供專(zhuān)業(yè)的網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè),龍南網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
<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" :>
<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>Vue是一款友好的、多用途且高性能的JavaScript框架,使用vue可以創(chuàng)建可維護(hù)性和可測(cè)試性更強(qiáng)的代碼庫(kù),Vue允許可以將一個(gè)網(wǎng)頁(yè)分割成可復(fù)用的組件,每個(gè)組件都包含屬于自己的HTML、CSS、JavaScript,以用來(lái)渲染網(wǎng)頁(yè)中相應(yīng)的地方,所以越來(lái)越多的前端開(kāi)發(fā)者使用vue。
關(guān)于怎么在vue 中利用自定義組件實(shí)現(xiàn)通訊錄功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
新聞標(biāo)題:怎么在vue?中利用自定義組件實(shí)現(xiàn)通訊錄功能
本文網(wǎng)址:http://www.chinadenli.net/article24/piedje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、用戶(hù)體驗(yàn)、域名注冊(cè)、網(wǎng)站改版、App設(shè)計(jì)、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)