百度小程序父子組件通信

父組件swan

<view id="registerList" class="copyrightRegisterIndex">

? <view bindtap="updateData">hehehehhe</view>

? <item-list id="start_id"? bindmyevent="onMyEvent" listData="{{listData}}" />

</view>


父組件 js

/**

* @file index.js

* @author swan

*/

const app = getApp()

Page({

? ? data: {

? ? ? ? listData:{},

? ? },

? ? onLoad() {

? ? ? ? // 監(jiān)聽(tīng)頁(yè)面加載的生命周期函數(shù)

? ? ? ? this.setData({

? ? ? ? ? ? listData:{

? ? ? ? ? ? ? ? name:'heihei'

? ? ? ? ? ? }

? ? ? ? })

? ? },

? ? onMyEvent: function (e) {

? ? ? ? console.log(e)

? ? },

? ? // 父頁(yè)面?zhèn)髦到o子組件

? ? updateData(){

? ? ? ? console.log("update")

? ? ? ? var start_id = this.selectComponent("#start_id");

? ? ? ? this.setData({

? ? ? ? ? ? listData:{

? ? ? ? ? ? ? ? name:'heihei1'

? ? ? ? ? ? }

? ? ? ? })

? ? ? ? start_id.btn_start(this.data.listData);

? ? }

})

父組件json

{

? "navigationBarTitleText":"組件通信",

? "usingComponents": {

? ? "item-list": "../components/index/workslist/workslist"

? },

? "pullRefresh":true

}


子組件swan

<view class="search-content-dd" bindtap="onTap">

? ? hahahhahah

</view>

子組件js

Component({

? ? properties: {

? ? ? ? listData:{},

? ? ? ? propName: { // 屬性名

? ? ? ? ? ? type: String, // 類型(必填),目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型)

? ? ? ? ? ? value: 'val', // 屬性初始值(必填)

? ? ? ? ? ? observer: function(newVal, oldVal) {

? ? ? ? ? ? ? ? // 屬性被改變時(shí)執(zhí)行的函數(shù)(可選)

? ? ? ? ? ? }

? ? ? ? }

? ? },

? ? data: {

? ? ? ? clearStatusValue:false,

? ? ? ? searchValue:'',

? ? ? ? styleWidth:{

? ? ? ? width:'686rpx'

? ? ? ? },

? ? ? ? copyrightRegisterData:[

? ? ? ? // {

? ? ? ? //? title:'作品名稱作品名稱作品名稱作品作品名稱作品名稱作品名稱作品',

? ? ? ? //? ownerName:'張三豐',

? ? ? ? //? commitTime:'2019-07-11 18:56',

? ? ? ? //? status:6,

? ? ? ? // },

? ? ? ? ],

? ? ? ? pageNumber:1,

? ? ? ? pageSize:10,

? ? }, // 私有數(shù)據(jù),可用于模版渲染

? ? // 生命周期函數(shù),可以為函數(shù),或一個(gè)在methods段中定義的方法名

? ? // 組件生命周期函數(shù),在組件實(shí)例進(jìn)入頁(yè)面節(jié)點(diǎn)樹(shù)時(shí)執(zhí)行

? ? attached: function () {

? ? ? ? console.log(this.properties.listData)

? ? },

? ? // 組件生命周期函數(shù),在組件實(shí)例被從頁(yè)面節(jié)點(diǎn)樹(shù)移除時(shí)執(zhí)行

? ? detached: function () {


? ? },

? ? methods: {

? ? ? ? // 子組件傳值給父頁(yè)面

? ? ? ? onTap:function() {

? ? ? ? ? ? console.log("11111111")

? ? ? ? ? ? var myEventDetail = {name:'hahhah'} // detail對(duì)象,提供給事件監(jiān)聽(tīng)函數(shù)

? ? ? ? ? ? this.triggerEvent('myevent', myEventDetail);

? ? ? ? },

? ? ? ? // 子組件接收父組件點(diǎn)擊事件傳過(guò)來(lái)的值

? ? ? ? btn_start: function(obj) {

? ? ? ? console.log("子組件接收到的值", obj);

? ? ? ? //? if (obj == 2) {

? ? ? ? //? ? this.setData({

? ? ? ? //? ? ? context: "子組件接收到父組件頁(yè)面點(diǎn)擊事件傳過(guò)來(lái)的值"

? ? ? ? //? ? })

? ? ? ? }


? ? }

});

子組件json

{

? ? "component": true,

? ? "usingComponents": {}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容