亚洲欧美日韩综合系列在线_91精品人妻一区二区_欧美大肥婆一级特大AA片_九色91视频免费观看_亚洲综合国产精品_av中文字幕在线不卡_久久精品色综合网_看黄色视频的软件_无卡无码高清中文字幕码2024_亚洲欧美日韩天堂网

微信小程序中組件通訊的介紹(代碼示例)

來源:不言 發(fā)布時(shí)間:2018-11-01 10:06:29 閱讀量:757

本篇文章給大家?guī)淼膬?nèi)容是關(guān)于微信小程序中組件通訊的介紹(代碼示例),有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)你有所幫助。

這篇主要講組件通訊

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

1

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

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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 直接就可以獲取到

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

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

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

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

1

2

3

4

onReady: function () {

    //獲得popup組件

    this.header= this.selectComponent("#header");

},

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

1

2

3

4

// 調(diào)用子組件方法

 fn(){

   this.header.fn() //子組件的方法

 },


標(biāo)簽: PHP
分享:
評(píng)論:
你還沒有登錄,請(qǐng)先