当前位置:网站首页>Communication between parent-child components and parent-child components provide and inject
Communication between parent-child components and parent-child components provide and inject
2022-07-29 03:58:00 【1 Jiajia Jiajia】
One 、 Concept
This pair of options is used together . To allow an ancestor component to inject a dependency into all its descendants , No matter how deep the component level is , And it will always take effect when the upstream downstream relationship is established .
provide: It's an object , Or a function that returns an object . And it contains the things that will be given to future generations , That is, attributes and attribute values .
inject: An array of strings , Or an object . Property value can be an object , contain from and default The default value is .
Two 、 scene
When a child component or grandchild component wants to use the variables of the parent component or needs to change the variable value of the parent component , Usually use props and $emit To implement . The interaction between parent and child components is also more convenient , But once the level of sub components becomes deeper , exceed 2 Level or above , Communication between components will no longer be convenient , To use provide And inject.
You can call the data of the parent component , You can also call the methods of the parent component
3、 ... and 、 example
// Parent component
<template>
<div>
{
{testData}} // ’ This is the data of the parent component ‘
<son></son>
</div>
</template>
export default {
// adopt provide Implement the sub component to update the data of the parent component , Call in the sub component
provide () {
return {
updateTestData: this.updateTestData,
testData: this.testData
}
},
data () {
return {
testData: ' This is the data of the parent component '
}
},
components: {
son: resolve => {
require(["@components/son"], resolve);
}
},
methods: {
// to update testData data
updateTestData(txt) {
this.testData += txt
}
}
}
// Child components
<template>
<div>
{
{testData}} // ’ This is the data of the parent component ‘
<grandson></grandson>
</div>
</template>
export default {
inject: ['testData'],
data () {
return {
}
},
components: {
grandson: resolve => {
require(["@components/grandson"], resolve);
}
}
}
// Sun component
<template>
<div>
</div>
</template>
export default {
inject: ['updateTestData'],
data () {
return {
}
},
created () {
setTimeout(() => {
this.updateTestData(', Now it has been changed by the sub component ')
}, 2000)
}
}
When the sub component is loaded successfully and executed ( The timer simulates the real execution scenario ,2 Seconds later ), Parent and child components testData Turned into 【 This is the data of the parent component , Now it has been changed by the sub component 】
边栏推荐
- 企业网的三层架构
- Li Kou daily question - day 44 -205. Isomorphic string
- Spark dataframe replaces empty characters (or other values) in each column with null
- How do programmers use code to completely end those things in the system?
- Why don't programmers work blindly?
- 小马智行进军前装量产,从自研域控制器入手?
- Batch production and upload sales NFT opensea eth polygon
- 1. Mx6u driver development-2-led driver
- Who can elaborate on the semi consistent read under mysqlrc and how to reduce the deadlock probability?
- Sunflower senior product director technology sharing: "how to apply national remote control" in AD domain environment
猜你喜欢

Configmap configuration and secret encryption

Microcomputer principle and interface technology

Getting started with caspin

Typescript from getting started to mastering (XVI) configuration file - first knowledge of compileroptions configuration item

向日葵远程控制为何采用BGP服务器?自动最优路线、跨运营商高速传输

Data mining -- Introduction to the basis of association analysis (Part 1)

RHCE的at,crontab的基本操作,chrony服务和对称加密和非对称加密
![[BGP] small scale experiment](/img/58/877e5e454e9bab9d1bccb8fdd3b04d.png)
[BGP] small scale experiment

Shopify卖家:EDM营销就要搭配SaleSmartly,轻松搞定转化率

HCIP BGP
随机推荐
消费行业数字化升级成 “刚需”,weiit 新零售 SaaS 为企业赋能!
1985-2020 (8 Editions) global surface coverage download and introduction
Shopify seller: EDM marketing should be combined with salesmartly to easily get the conversion rate
无法一次粘贴多张图片
Excel splicing database statement
Typescript from getting started to mastering (XXII) namespace namespace (I)
The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!
Why does the 20 bit address bus determine the storage space of 1MB
OA项目之会议通知(查询&是否参会&反馈详情)
代码 ~ 隐藏或禁用状态栏和虚拟按键
Typescript from getting started to mastering (19) enumeration types
What you see and think in Microsoft
华为天才少年稚晖君做了一把模块化机械键盘,引起极客圈地震,网友:这才是真正的客制化...
Typescript from getting started to mastering (XVI) configuration file - first knowledge of compileroptions configuration item
Common methods of lodash Library
Object array merges elements according to a field
Cloudera manager platform fault repair record
【深度学习CPU(番外篇)——虚拟内存】
nacos注册中心
UCOS task switching process