当前位置:网站首页>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 …

边栏推荐
- @RequestBody解决获取参数为null
- Hello CTP (II) -- Introduction to CTP
- acmStreamOpen返回值问题
- 文本关键词提取:ansj
- [openwrt] we recommend a domestically developed version of openwrt, an introduction to istoreos. It is very easy to use. It is mainly optimized. It solves the problem of Sinicization.
- Upgrade cmake
- Changsha's "talent seeking": "making efforts" and "making practical moves" go hand in hand, "rapid development" and "slow life" go hand in hand
- Laravel document sorting 2. Route related
- 1. Phase II of the project - user registration and login
- MySQL插入过程报错1062,但是我没有该字段。
猜你喜欢

文本关键词提取:ansj

Numpy NP tips: use OpenCV to interpolate and zoom the array to a fixed shape cv2 resize(res, dsize=(64, 64), interpolation=cv2. INTER_ CUBIC)

Watch out for the stolen face! So many risks of face recognition used every day?

Turn 2D photos into 3D models to see NVIDIA's new AI "magic"!

navicat可不可以直接操作安卓数据库SQLite

Laravel document sorting 4. Controller

Hello CTP (II) -- Introduction to CTP

Finereport displays and hides column data according to conditions

Read lsd-slam: large scale direct monolithic slam

Is opencv open source?
随机推荐
关于TCP连接四次握手(或者叫四次挥手)的详细总结
Coinlist how to operate the middle lot number security tutorial
Where is the red area of OpenCV?
Is opencv open source?
Read lsd-slam: large scale direct monolithic slam
"Comment positionner l'industrie" dans la planification industrielle locale / parc
Vigilance against over range collection of privacy - ten mobile app violations
NFT insider 63: the sandbox reached a cooperation with Time magazine, and YGG established Spain's subdao
Laravel document sorting 8. Middleware
PHP extracts and analyzes table contents, and collects bidding information
Laravel document sorting 11. System architecture
1. first knowledge of chromatic harmonica
mysql的tinyint字段类型判断的疑惑
numpy np tips:使用opencv对数组插值放缩到固定形状 cv2.resize(res, dsize=(64, 64), interpolation=cv2.INTER_CUBIC)
如何绘制产业招商地图
SQL, CTE, FLG CASE问题
The 5th series of NFT works of missing parts was launched on the sandbox market platform
代錶多樣性的彩色 NFT 系列上線 The Sandbox 市場平臺
Smart contract learning materials
【LeetCode】22. bracket-generating