這篇主要講組件通訊
(1)父組件向子組件傳值:
<header title='{{title}}' bind:fn='fn' id='header'></header>通過title='{{title}}'傳向子組件向子組件傳遞參數
子組件接收參數:
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函數接受三個值:事件名稱、數據、選項值 } }})methods使用title時 this.data.title 直接就可以獲取到
通過 bind:fn='fn'傳向子組件向子組件傳遞方法
方法同樣也要在properties接收,methods里定義一個新方法, this.triggerEvent("fn") 接收父組件傳遞過來的方法
(2)父組件調用子組件數據及方法:
首先在父組件js onReady 生命周期中獲取到組件
onReady: function () { //獲得popup組件 this.header= this.selectComponent("#header");},比如要調用子組件的一個function方法
// 調用子組件方法 fn(){ this.header.fn() //子組件的方法 },調用子組件數據的話直接 this.header.msg 就可以拿到子組件的數據
新聞熱點
疑難解答