当前位置:网站首页>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 】
边栏推荐
- 1. 头文件-注释-命名空间-标准输入输出流
- Li Kou daily question - day 44 -205. Isomorphic string
- 无法一次粘贴多张图片
- How fast does it take to implement a super simple language
- Shutter start white screen
- Interview essential! TCP classic 15 consecutive questions!
- Typescript from getting started to mastering (XX) function generics
- Vs code must know and know 20 shortcut keys!
- 数据挖掘——关联分析例题代码实现(下)
- 《陌路曾相逢》夏陌沈疏晏_夏陌沈疏晏最新章节
猜你喜欢

企业网的三层架构

(newcoder 15079) irrelevant (inclusion exclusion principle)

消费行业数字化升级成 “刚需”,weiit 新零售 SaaS 为企业赋能!

Zhihuijun, a genius of Huawei, made a modular mechanical keyboard, which caused an earthquake in the geek circle. Netizens: This is the real customization

The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!

RHCE's at, crontab's basic operations, the Chrony service, symmetric encryption and asymmetric encryption

Shutter start white screen

What have I learned from 200 machine learning tools?

Basic configuration of BGP - establish peers and route announcements

Getting started with caspin
随机推荐
关于双指针的思想总结
The list is not updated in real time when JS V-for data changes
How fast does it take to implement a super simple language
Ssl== certificate related concepts
@Configuration (proxybeanmethods = false) what's the use of setting this to false
String template of ES6 new features and methods to simplify objects and functions
MySQL Part 4 (end)
座机的表单验证
RHCE's at, crontab's basic operations, the Chrony service, symmetric encryption and asymmetric encryption
Zhihuijun, a genius of Huawei, made a modular mechanical keyboard, which caused an earthquake in the geek circle. Netizens: This is the real customization
Malloc C language
tron OUT_ OF_ ENERGY
UCOS任务切换过程
CUB_ Visualization of key points in 200 bird dataset
通过PWM呼吸灯和PWM控制直流电机来详细介绍TIM的输出比较功能
Typescript from getting started to mastering (XXII) namespace namespace (I)
C language to achieve three chess game (detailed explanation)
C语言实现三子棋游戏(详解)
MySQL Part 3
BGP的基础配置---建立对等体、路由宣告