本篇文章给大家带来的内容是关于微信小程序中组件通讯的介绍(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
这篇主要讲组件通讯 (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 就可以拿到子组件的数据 以上就是微信小程序中组件通讯的介绍(代码示例)的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章! |