給子組件添加一個id屬性,方便查找
<view>
<first-component id="mycomp"/>
</view>
1
2
3
通過this.selectComponent方法查找子組件
Page({
/**
* 頁面的初始數據
*/
data: {
},
/**
* 生命周期函數--監聽頁面初次渲染完成
*/
onReady: function () {
console.log(this);
// 獲取子組件的數據
console.log(this.selectComponent("#mycomp").data);
// 調用子組件的方法
this.selectComponent("#mycomp").huanghaili()
debugger
}
})
1
2
3
4
調用子組件方法 this.selectComponent("#mycomp").huanghaili()
查找子組件的屬性 this.selectComponent("#mycomp").data
子組件定義的邏輯
Component({
/**
* 組件的屬性列表
*/
properties: {
},
/**
* 組件的初始數據
*/
data: {
msg: 'i am msg'
},
/**
* 組件的方法列表
*/
methods: {
huanghaili: function () {
console.log("huanghaili")
}
}
})