当前位置:网站首页>自定义组件--父子组件之间的通信
自定义组件--父子组件之间的通信
2022-07-28 05:25:00 【像费曼%】
1.父子组件之间通信的3种方式
- 属性绑定
用于父组件向子组件指定属性设置数据,仅能设置json兼容的数据
- 事件绑定
用于子组件向父组件传递数据,可以传递任意数据
- 获取组件实例
父组件还可以通过this.selecteComponent()获取子组件实例对象
这样就可以直接访问子组件中的任意数据和方法
2.属性绑定
属性绑定用于实现父向子传值,而且只能传递普通类型的数据,无法将方法传递给子组件。
//父组件的data节点
data: {
count:9
},
//父组件的wxml结构
<test2 count="{
{count}}">
<button size="mini" slot="before">点击</button>
<input type="text" slot="after" style="background-color:rgb(112, 211, 211);"/>
</test2>
//使用
//子组件的properties节点
properties: {
count:{
type:Number,
value:0
}
},
//子组件的wxml结构
<view>
<slot name="before"></slot>
<view>这是分割线——————————————————******</view>
<slot name="after"></slot>
<view>{
{count}}</view>//使用
</view>3.事件绑定
事件绑定用于实现子向父传值,可以传递任何类型的数据,使用步骤如下:
//步骤一
syncCount(){
console.log(' syncCount')
},
//步骤二:
<test2 count="{
{count}}" bind:sync="syncCount">
<button size="mini" slot="before">点击</button>
<input type="text" slot="after" style="background-color:rgb(112, 211, 211);"/>
</test2>
//步骤三
//组件wxml
<button type="primary" bindtap="addCount">+1</button>
//组件js
addCount(){
this.setData({
count:this.properties.count+1
})
this.triggerEvent('sync',{value:this.properties.count})
}
}
//步骤四
syncCount(e){
this.setData({
count:e.detail.value
})
},
4.获取组件实例
可在父组件里调用this.selectComponent("class或id选择器"),获取子组件的实例对象,从而直接访问子组件的任意数据和方法。调用时需要传入一个选择器,例如 this.selectComponent(".my-component")

//父组件js
getChild(){
const child = this.selectComponent('.customA')
// console.log(child)
child.setData({
count:child.properties.count+1//调用子组件的setData方法
})
child.addCount()//调用子组件的addCount方法
},代码结果:

边栏推荐
- 多个ics日历合并成单个ics日历
- set_ case_ analysis
- Detailed explanation of word mail merge function: after merging, multiple word documents are generated and blank pages are deleted
- PyTorch 学习笔记 4 —— 自动计算梯度下降 AUTOGRAD
- EfficientNET_ V1
- 当mysql表从压缩表变成普通表会发生什么
- Learning notes of hardware circuit design 1 -- temperature rise design
- 【服务器使用记录】通过跳板机登录远程服务器并进行文件传输
- Pytorch learning notes 1 - quick start
- An example of bill printing
猜你喜欢

mysql join技巧

Matlab simulation of radar imaging 1 - LFM signal and its spectrum

clickhouse建宽表多少列最合适?

Measure computer battery capacity

Word自动目录字体修改和行间距的问题

Photovoltaic power generation system MPPT maximum power point tracking

Pycharm2019设置编辑器主题和默认代码

OpenGL快速配置方法

详解安装msdn 2015及其注意事项

Triode design, understanding saturation, linear region and cut-off region
随机推荐
Matlab simulation of radar imaging 1 - LFM signal and its spectrum
ICC2分析时序的神器 analyze_design_violations
Electric fast burst (EFT) design - EMC series hardware design notes 4
USB network native driver for esxi updated to support esxi7.0 Update 2
EfficientNET_ V1
Esxi on arm 10/22 update
解决内存占用比应用进程占用高的问题
RS232 RS485 RS422 communication learning and notes
OpenGL的开发环境配置【VS2017】+常见问题
TVs tube parameters and selection
低功耗设计-isolation cell
qt中Qthread线程的使用以及安全关闭
Varistor design parameters and classic circuit recording hardware learning notes 5
set_ clock_ groups
Photovoltaic power generation system MPPT maximum power point tracking
论文神器 VS Code + LaTex + LaTex Workshop
Design and analysis of contactor coil control circuit
Pytorch learning notes
qt批量操作控件,并设置信号槽
Graduation thesis | how to write literature review