小程序中類似Vue的ref獲取組件內部屬性和方法


給子組件添加一個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")
}
}
})


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 Vue中使用ref屬性獲取元素||組件標簽 React Native中ref的用法(通過組件的ref屬性,來獲取真實的組件) vue3