当前位置:网站首页>Value transfer between parent and child components of wechat applet
Value transfer between parent and child components of wechat applet
2022-06-25 04:27:00 【But do good WLW】
Pass values between parent and child components
There are two types of value transfer between components , One is Pass value from father to son , The other is Pass from son to father
Let's talk about the general difference first …
difference :
- The parent passes values to the child using
Attribute binding, Sub components Ofpropertiesobject Receive the value passed by the parent component - When a child passes a value to a parent, it uses
Custom events, The parent component uses the... In the custom eventEvent object eTo receive the value passed by the sub component
Pass value from father to son
First we create a in our applet components Folder Used to store our sub components . After the creation, we can components Folder to create our sub components child , Of course, any name can be created , I created here child , Here's the picture :
After the creation, it is introduced , Put this Child components child Introduced to us Parent component index Go to the home page . Here's an explanation , I am here altogether It was created. Two pages , One is pages Node under index Routing page , as well as components Node under child Components .
So how do we introduce ?( The principle is in the parent component json In the document usingComponents Under the node introduce ) as follows :
{
"usingComponents": {
"child":"../../components/child/child"
}
}
How to put Child components The content of Render to parent component How about it ?… :
It's also very simple. : When we introduced it above , Look at the code above : The custom name given to the component is child that When we render stay index Parent component Write a group in <child><child> Labels can be , You can render the sub components to Parent component …
At this point, we have a parent-child page , Then you can transfer values …
We have data in the parent component data Two data are defined in ,name:' Zhang San ', age:23, Pass this data to Child components child
Here we have three steps :
1. stay child In the label Use attribute binding Bind the value of the parent component <child name="{ { name }}" age="{ { age }}" ></child>, At this point, the operation of the parent component has ok 了 , Next, let's look at the operation of sub components …
2. In subcomponent js Of documents properties Object to receive the value passed by the parent component
// Accept the value passed by the parent component
properties: {
name:{
type:String,
value:' I'm the default : Apple '
},
age:{
type:Number,
value:' I'm the default : 100'
}
},
3. At this point, the subcomponents child It's been received The value passed by the parent component , We can do it in Subcomponents wxml On the page Refer to this value to see if the transfer is successful
<!-- Pass value from father to son -->
<view>
This is the value passed by the parent component name: {
{ name }}-----age {
{age}}
</view>
index Parent page The results are as follows :

Obviously the delivery was successful … >^<
Next, let's talk about the value transfer from the child to the parent
Pass from son to father
Let's start with the subcomponent data In the data Define a data msg
data: {
msg:' I am the value of the subcomponent '
},
Next, start to transfer values , There are two kinds of value transfer time ,
The first is in the life cycle of the build , That is, as soon as the page is loaded, the value in the component will be passed
The second way is to pass values through event binding
Let's start with the second :( The way of time binding ):
1. On sub components wxml Put a button in the middle , And bind him to an event send
<!-- Click the button to transfer the value to the parent component -->
<button bindtap="send"> Click the button to transfer the value to the parent component </button>
2. stay child.js Define this in send , And write the code for value transmission :
methods: {
send(){
this.triggerEvent('send',this.data.msg)
}
},
Value transfer uses :this.triggerEvent() , In the above code send Is the name of the custom event passed to the parent component , Next, define this custom event in the parent component bindsend="send"
<child name="{
{ name }}" age="{
{ age }}" bindsend="send"></child>
When it's defined , stay Parent component js In file , Define it send , And give send Method , Pass a Event object e
// The parent component accepts the value passed by the child component
send(e){
console.log(e.detail);
},
The values passed by the subcomponents are stored in e.detail in …

边栏推荐
- Development of trading system (IX) -- dark pool technology
- Laravel document sorting 1. Installation and Preliminary Configuration
- Shutter fittedbox component
- DAP data scheduling function improvement description
- Detailed explanation of flex attributes in flex layout
- PostgreSQL数据库WAL——RM_HEAP_ID日志记录动作
- NFT Insider #63:The Sandbox与时代杂志达成合作,YGG成立西班牙subDAO
- Mathematical analysis_ Notes_ Chapter 3: limits
- La gamme NFT Color, qui représente la diversité, est en ligne sur la plate - forme du marché Sandbox
- Nodejs connects to MySQL through heidisql, and ER appears_ BAD_ DB_ ERROR: Unknown database 'my_ db_ books'
猜你喜欢

Acmstreamopen return value problem

Coinlist how to operate the middle lot number security tutorial

微信小程序父子组件之间传值

《Missing Parts》NFT 作品集第 5 系列上线 The Sandbox 市场平台

地方/園區產業規劃之 “ 如何進行產業定比特 ”

警惕超范围采集隐私-移动APP违规十宗罪

马斯克发布人形机器人,AI对马斯克为什么意义重大?

Hello CTP (III) - CTP quotation API

English Grammar - pronunciation rules
![Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding](/img/ad/69fce7cf064479a0ddd477fb935de2.png)
Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding
随机推荐
La gamme NFT Color, qui représente la diversité, est en ligne sur la plate - forme du marché Sandbox
Synchronous and asynchronous functions (callback function, promise, generator, async/await)
CMD operation MySQL in Windows
kenlm
Laravel document sorting 10. Request life cycle
Simple integration of client go gin -update
Hello CTP (I) - basic knowledge of futures
English Grammar - pronunciation rules
SQL, CTE, FLG CASE问题
95% 程序员都在这里摸鱼……
Exercise: how difficult is it to simulate the blessing lottery two-color ball - China 500W grand prize? Just run the code.
小心被偷脸!天天用的人脸识别风险原来这么多?
The 5th series of NFT works of missing parts was launched on the sandbox market platform
Changsha's "talent seeking": "making efforts" and "making practical moves" go hand in hand, "rapid development" and "slow life" go hand in hand
2. play the chromatic harmonica
Shutter fittedbox component
1280_ C language to find the average value of two unsigned integer
什么是持久化?redis 持久化中的RDB和AOF是什么?
Smart contract learning materials
Sourcetree pulls the code and prompts to fill in authentic, but the configuration cannot change the user