亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

這篇主要講組件通訊

(1)父組件向子組件傳值:

<header title='{{title}}' bind:fn='fn' id='header'></header>

通過(guò)title='{{title}}'傳向子組件向子組件傳遞參數(shù)

子組件接收參數(shù):

Component({
 properties: {
  title: {    // 屬性名 type: Number, // 類型(必填)
   type: String,//目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型)
  },
  fn: {   
   type: Function,
  },
 },
 data: {
    
 },
 methods: {
  // 子組件調(diào)用父組件方法
  childFn() {
   console.log(this.data.title)
   this.triggerEvent("fn");
   //triggerEvent函數(shù)接受三個(gè)值:事件名稱、數(shù)據(jù)、選項(xiàng)值 
  }
 }
})

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

通過(guò) bind:fn='fn'傳向子組件向子組件傳遞方法

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

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

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

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

比如要調(diào)用子組件的一個(gè)function方法

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

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

相關(guān)文章

最新評(píng)論