当前位置:网站首页>自定义组件--父子组件之间的通信
自定义组件--父子组件之间的通信
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方法
},代码结果:

边栏推荐
- PyTorch 学习笔记 4 —— 自动计算梯度下降 AUTOGRAD
- Pycharm2019 set editor theme and default code
- Redhawk Dynamic Analysis
- An example of bill printing
- USB network native driver for esxi updated to support esxi7.0.1
- ICC2分析时序的神器 analyze_design_violations
- Bert bidirectional encoder based on transformer
- Cronbach’s α?KMO系数?因子载荷?史上最易懂的问卷信效度分析教程!!!(SPSS和AMOS)
- I heard that you are also practicing when I interviewed several junior interns.
- clickhouse聚合之内存不足怎么办?那就提升聚合性能
猜你喜欢

Low power design -power switch

Arduino reads the analog voltage_ How mq2 gas / smoke sensor works and its interface with Arduino

qt中获取当前目录

ICC2分析时序的神器 analyze_design_violations

I heard that you are also practicing when I interviewed several junior interns.

Matlab simulation of radar imaging 4 - range resolution analysis

vi和vim命令

Measure computer battery capacity

雷达成像 Matlab 仿真 4 —— 距离分辨率分析

An example of bill printing
随机推荐
Hugging face 的问题记录 I
qt解决每次修改完ui文件都要重新构建的问题
Detailed explanation of word mail merge function: after merging, multiple word documents are generated and blank pages are deleted
多个ics日历合并成单个ics日历
How to view the transfer function of the module directly built by Simulink
WebService error maximum message size quota for incoming messages (65536) has been exceeded
ASP. Net read database bound to treeview recursive mode
ConNeXt
An example of bill printing
PyTorch 学习笔记
Bag of tricks training convolution network skills
Word邮件合并功能详解:合并后生成多个word文档,删除空白页
How to test industrial Ethernet cables (using fluke dsx-8000)?
详解安装msdn 2015及其注意事项
一、ffmpeg录制音频为pcm文件
set_multicycle_path
Vs code basic configuration and beautification
mixup_ratio
T-sne dimension reduction visualization
OpenGL快速配置方法