今天把印象笔记里的笔记整理上来了一下,可能会漏点字母什么的
1.子组件传给父组件 需事件/方法触发
// 子组件
childFunc(e){
this.triggerEvent('childData', { childId: this.data.childId})
}
//父组件
html:
<childDemo bind:childData='childData' ></childDemo>
js:
childData(e){
const { childId} = e.detail
}
2.父组件给子组件传值
//父组件
html:
<childDemo parentData='parentData' ></childDemo>
js:
data:{
parentData: ‘zff’
}
子组件
properties: {
parentData: {
type: 'String',
value: ''
}
},
lifetimes: {
// 生命周期函数,可以为函数,或一个在methods段中定义的方法名
ready: function () {
//执行获取this.data.parentData方法
}
},
3.父组件 点击后 传给子组件值
父组件
parentTap(){
var childDemo = that.selectComponent("#childDemo");
childDemo.sendParentData(//要传的值);
}
子组件接收
sendParentData: function (data){
console.log(data)
}