線上服務(wù)咨詢
Article/文章
記錄成長點(diǎn)滴 分享您我感悟
微信小程序中組件通訊的介紹(代碼示例)
本篇文章給大家?guī)淼膬?nèi)容是關(guān)于微信小程序中組件通訊的介紹(代碼示例),有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)你有所幫助。
這篇主要講組件通訊
(1)父組件向子組件傳值:
<header title='{{title}}' bind:fn='fn' id='header'></header>
通過title='{{title}}'傳向子組件向子組件傳遞參數(shù)
子組件接收參數(shù):
Component({ properties: { title: { // 屬性名 type: Number, // 類型(必填) type: String,//目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型) }, fn: { type: Function, }, }, data: { }, methods: { // 子組件調(diào)用父組件方法 childFn() { console.log(this.data.title) this.triggerEvent("fn"); //triggerEvent函數(shù)接受三個(gè)值:事件名稱、數(shù)據(jù)、選項(xiàng)值 } app開發(fā)}})
methods使用title時(shí) this.data.title 直接就可以獲取到
通過 bind:fn='fn'傳向子組件向子組件傳遞方法
方法同樣也要在properties接收,methods里定義一個(gè)新方法, this.triggerEvent("fn") 接收父組件傳遞過來的方法
(2)父組件調(diào)用子組件數(shù)據(jù)及方法:
首先在父組件js onReady 生命周期中獲取到組件
onReady: function () { //獲得popup組件 this.header= this.selectComponent("#header");},
比如要調(diào)用子組件的一個(gè)function方法
// 調(diào)用子組件方法 fn(){ this.header.fn() //子組件的方法 },
調(diào)用子組件數(shù)據(jù)的話直接 this.header.msg 就可以拿到子組件的數(shù)據(jù)
以上就是微信小程序中組件通訊的介紹(代碼示例)的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注沈陽網(wǎng)站建設(shè)其它相關(guān)文章!
components,微信小程序