欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解微信小程序中組件通訊

 更新時間:2018年10月30日 08:36:39   投稿:laozhang  
在本篇文章里我們給大家分享了微信小程序中組件通訊的相關知識點以及相關實例代碼,有興趣的朋友們學習分享下。

這篇主要講組件通訊

(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: {
  // 子組件調用父組件方法
  childFn() {
   console.log(this.data.title)
   this.triggerEvent("fn");
   //triggerEvent函數(shù)接受三個值:事件名稱、數(shù)據(jù)、選項值 
  }
 }
})

methods使用title時 this.data.title 直接就可以獲取到

通過 bind:fn='fn'傳向子組件向子組件傳遞方法

方法同樣也要在properties接收,methods里定義一個新方法, this.triggerEvent("fn") 接收父組件傳遞過來的方法

(2)父組件調用子組件數(shù)據(jù)及方法:

首先在父組件js onReady 生命周期中獲取到組件

onReady: function () {
  //獲得popup組件
  this.header= this.selectComponent("#header");
},

比如要調用子組件的一個function方法

// 調用子組件方法
 fn(){
  this.header.fn() //子組件的方法
 },

調用子組件數(shù)據(jù)的話直接 this.header.msg 就可以拿到子組件的數(shù)據(jù)

相關文章

最新評論