小程序-子組件調(diào)用父組件的方法

準(zhǔn)備工作 ----- 封裝組件
image.png
子組件

a-tt.wxml

 <view bindtap="parentComponentFunction">
    <button>子調(diào)用父的方法</button>
</view>

a-tt.json

{
  "component": true,
  "usingComponents": {}
}

a-tt.js

// components/test.js
Component({
  /* 組件的屬性列表 */
  properties: {
   
  },
  /* 組件的初始數(shù)據(jù) */
  data: {

  },
  /* 組件的方法列表 */
  methods: {
    parentComponentFunction: function(){
      this.triggerEvent('parentComponentFunction');
  //   console.log("獲取父級方法")
    }
  }
})
父組件

index.wxml

 <view>
   <a-tt bind:parentComponentFunction="parentComponentFunction" />
 </view>

index.json

{
  "usingComponents":{
    "a-tt":"/components/a-tt/a-tt"
  }
}

js

//index.js
//獲取應(yīng)用實(shí)例
const app = getApp()

Page({
  data: {
    
  },
  parentComponentFunction: function(){
    console.log("成功調(diào)用父組件的方法");
    // 可以寫方法
    // wx.navigateTo({
    //   url : ' '
    // })
  }
})
控制臺顯示
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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