您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關怎么在解微信小程序中通過自定義組件實現數據交互,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
step1:創建文件夾以及文件
首先創建一個components(名字可自取)文件夾,用于放置所有自定義的組件,
里面可以用右鍵自動創建的方式,新建一個Component組件,例如:
創建之后的目錄結構為:
其中callphone是我們本次要實現的撥打電話組件。
step2:組件的基本搭建
在callphone.wxml中:添加組件結構:
<view class="call" bindtap="callPhone"> <view class="phone">{{phone}}</view> <view>了解最新信息,免費咨詢</view> <view class="callIcon"></view> </view>
在callphone.wxss中:添加組件樣式:
.call { display: block; padding: 18rpx 30rpx; width: 100%; color: #999; font-size: 24rpx; box-sizing: border-box; background: #ecf9e8; position: relative; border-radius: 10rpx; } .call .phone { color: #333; font-size: 40rpx; } .call .callIcon { width: 77rpx; height: 77rpx; position: absolute; top: 50%; margin-top: -38rpx; right: 30rpx; background-size: cover; background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAE0AAABNCAYAAADjCemwAAALaUlEQVR4Xu1ce3BU1Rn/fecmmxCIDwoqOI5PEmDvwlh8dQwkuyAx2Y3TqmDt+NbOtIpax9Y61VpatdM60hkr7Yzjo1U6rYJYdbOLEbI3RHwWamFvwKC2tpZgCxUhEpNN9nydc7MJm7DJ3t3cJYuz58/kO9/5zm/PPed7E8Zp1L82s4JixXMZslIQVUhghgCOZ3A5QOUAygB0AdxJoE4J7BPA+5J5J0G0s6t3a3j+ezvHQ3w6UovWtZ49VfT1NoDgBbMPoOljX5s7QBQBw5BFxcF1C97dM3ae6TnkFLQa47TSiVTewMxXg3ExAcWDIhF2E/A2g9qJ0B6XcicVF+0p5VinFhed2DP7C0zdPiGuyfJucpVzb99UTYgKZlQSuJKB88GYNsCPgV4QXiGiVQe5M9ji/ag7/fazo8gJaIvWn3GsS0y4lQi3AzRFiTawKQFuisdhrFvUtj07kQ/Nqtvgnq1p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9qxUR3hQtdrdKDme/90zguVlgVwvnSPy0Z0kKnvvQHLCVFoSdklsANeoCHdR8FrC4yTar7KAuKOFGD5d+ozyLJvqO6++hwnAf63FKcr5GE2rR9bmXRnrgr1nlMUp/bA5os12KuchZf8j63qQ5poaPyGD7dgamH9e4mzBCsenervt2qf/dA727qJHCnJOwTnB+9u/8PGdpORDiwhy4AAAAASUVORK5CYII=); }
在callphone.js中:添加組件邏輯:
Component({ /** * 組件的屬性列表 */ properties: { }, /** * 組件的初始數據 */ data: { phone: '400-010-9797' }, /** * 組件的方法列表 */ methods: { callPhone() { wx.makePhoneCall({ phoneNumber: this.data.phone }) } } })
step3:使用組件
我們要在index.wxml中使用該組件的話,要在index.json中進行聲明:(其中call-phone是組件的名稱,可以自取)
{ "usingComponents": { "call-phone": "/components/CallPhone/callphone" } }
在index.wxml中引用組件:
<call-phone></call-phone>
完成以上幾個步驟,一個簡單的小程序自定義組件就完成了,運行結果如下:
Page與Component數據交互
上面的例子中可以看到,電話號碼是在組件中的data中設置的,也就是每個頁面都是固定寫死的;但是如果要實現每個界面的號碼不一樣的話,此時就涉及到page往組件傳值的問題,可以這么修改:
1、page > component
方法1:page在引用組件時能通過屬性值設置,callphone.js在properties獲取:
<!--index.wxml--> <call-phone phone="400-010-9797"></call-phone> // components/CallPhone/callphone.js Component({ properties: { phone: String //簡寫 /* phone: { type: String, //類型,目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型) value: '', //初始值 observer: function(newVal, oldVal) {} } */ }, attached: function() { console.log("this.properties.phone:" + this.properties.phone); //控制臺打印:"400-010-9797" }, //... })
方法2:page在引用組件時通data-xxx設置,callphone.js在this.dataset獲取:
<!--index.wxml--> <call-phone data-tel="400-010-9797" ></call-phone> // components/CallPhone/callphone.js Component({ attached: function() { console.log("this.dataset.tel:" + this.dataset.tel); //控制臺打印:"400-010-9797" // 設置properties值用setData() this.setData({ phone: this.dataset.tel }); }, //... })
2、component > page
組件中的變量要傳到page頁面,可以通過事件觸發this.triggerEvent()來實行:
// components/CallPhone/callphone.js Component({ methods: { callPhone() { var myEventDetail = { msg: '來自component的信息', tel: '400-010-9797' } // detail對象,提供給事件監聽函數 var myEventOption = {} // 觸發事件的選項 this.triggerEvent('myevent', myEventDetail, myEventOption) } } }) <!--index.wxml--> <!--"bindmyevent"屬性與上面"myevent"對應,當自定義組件觸發"myevent"事件時,調用"call"方法 --> <call-phone bindmyevent="call"></call-phone> //index.js Page({ call: function(e) { console.log(e.detail.msg);//控制臺打印:"來自component的信息" wx.makePhoneCall({ phoneNumber: e.detail.tel }) } })
以上就是怎么在解微信小程序中通過自定義組件實現數據交互,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。