当前位置:网站首页>Custom component -- communication between parent and child components
Custom component -- communication between parent and child components
2022-07-28 06:56:00 【Like Feynman%】
1. Communication between parent and child components 3 Ways of planting
- Attribute binding
It is used for the parent component to specify attribute setting data to the child component , You can only set json Compatible data
- Event binding
Used by child components to pass data to parent components , You can pass any data
- Get component instance
The parent component can also use this.selecteComponent() Get sub component instance object
In this way, you can directly access any data and methods in the sub components
2. Attribute binding
Property binding is used to pass values from parent to child , And can only pass ordinary types of data , Cannot pass method to child component .
// Parent component data node
data: {
count:9
},
// Parent component wxml structure
<test2 count="{
{count}}">
<button size="mini" slot="before"> Click on </button>
<input type="text" slot="after" style="background-color:rgb(112, 211, 211);"/>
</test2>
// Use
// Subcomponents properties node
properties: {
count:{
type:Number,
value:0
}
},
// Subcomponents wxml structure
<view>
<slot name="before"></slot>
<view> This is the dividing line ——————————————————******</view>
<slot name="after"></slot>
<view>{
{count}}</view>// Use
</view>3. Event binding
Event binding is used to pass values from child to parent , Can pass any type of data , The procedure is as follows :
// Step one
syncCount(){
console.log(' syncCount')
},
// Step two :
<test2 count="{
{count}}" bind:sync="syncCount">
<button size="mini" slot="before"> Click on </button>
<input type="text" slot="after" style="background-color:rgb(112, 211, 211);"/>
</test2>
// Step three
// Components wxml
<button type="primary" bindtap="addCount">+1</button>
// Components js
addCount(){
this.setData({
count:this.properties.count+1
})
this.triggerEvent('sync',{value:this.properties.count})
}
}
// Step four
syncCount(e){
this.setData({
count:e.detail.value
})
},
4. Get component instance
Can be called in the parent component this.selectComponent("class or id Selectors "), Get the instance object of the subcomponent , So as to directly access any data and methods of sub components . When calling, you need to pass in a selector , for example this.selectComponent(".my-component")

// Parent component js
getChild(){
const child = this.selectComponent('.customA')
// console.log(child)
child.setData({
count:child.properties.count+1// Call the setData Method
})
child.addCount()// Call the addCount Method
},Code results :

边栏推荐
- DHCP原理与配置
- Installation and configuration of unit test framework jest with typescript
- What's a good gift for Tanabata? Niche and advanced product gift recommendation
- [the beginning of self redemption]
- Pku-2524-ubiquitous relations (parallel search template)
- DNS domain name resolution service
- JS reverse question 100 - question 1
- Life cycle (process) of software testing
- [C language] custom structure type
- Common models in software development
猜你喜欢

修复故障扇区

Applets: WSX scripts

DHCP原理与配置
![[explain in detail how to realize Sanzi chess step by step]](/img/17/68ef51ec2be0c86019461116ecaa82.png)
[explain in detail how to realize Sanzi chess step by step]

SSH服务配置

Technology sharing | sending requests using curl

What kind of air conduction Bluetooth headset with good configuration is recommended

DNS forward resolution experiment

NAT和PAT的原理及配置

Si Han talks about the professional development of testers
随机推荐
VMware Workstation 配置net模式
Scratch command
It is recommended to wear air conduction earphones, which do not need to wear in ear
MySQL master master
小tips
什么是线性表?
Test interview questions collection (I) | common required questions and procedures of software testing (with answers)
HDU-5806-NanoApeLovesSequenceⅡ(尺取法)
Principle and configuration of NAT and pat
网络——传输层(详细版)
Technology sharing | interface testing value and system
SSH服务配置
archery数据库审核平台部署
思寒漫谈测试人职业发展
DNS正向解析实验
Traversal binary tree
Gerapy use
Hdu-2036-reform spring breeze blowing all over the ground (polygon area template)
Compilation and preprocessing of C language
OSI seven layer model