父传子
父组件中的json文件
{
"component": true,
"usingComponents": {
"componentB": "../child2/child2"
}
}
第二步是在父组件中的wxml中使用子组件中模块
<view>
<view>子组件内容:</view>
<componentB customAttribute='我是A向B中传入的参数'/>
</view>
在子组件中:
Component({
behaviors: [],
properties: {
paramAtoB:String //声明父组件传过来的类型为string类型
},
data: {
}, // 这里放的是子组件的私有数据,可用于模版渲染
// 生命周期函数,可以为函数,或一个在methods段中定义的方法名
attached: function () { },
moved: function () { },
detached: function () { },
methods: {
}
})
在父组件中使用:
<view>父组件中传来的参数:{{customAttribute}}</view>
子传父
子组件中的wxml:
<view>
<button bindtap='customEvent'>向A中传入参数</button>
</view>
12345
子组件中的js
Component({
behaviors: [],
properties: {
},
data: {
},
attached: function () { },
moved: function () { },
detached: function () { },
methods: {
change:function(){
this.triggerEvent('myevent', { msg:123});
}
}
})
父组件中接受参数:
<componentB paramAtoB='我是A向B中传入的参数' bind:myevent="onMyEvent"/>
123
事件执行,通过e.detail.msg获取到子传递过来的数据
Component({
behaviors: [],
properties: {
},
data: {
},
attached: function () { },
moved: function () { },
detached: function () { },
methods: {
onMyEvent:function(e){
console.log(e.detail.msg) \\e.detail.paramBtoA这就是子传父传递过来的数据
}
}
})
转载请注明原文地址:https://blackberry.8miu.com/read-14244.html