当前位置:网站首页>Communication between non parent and child components
Communication between non parent and child components
2022-07-07 07:09:00 【To be a woman of light】
Here we mainly talk about two ways :
- Provide/Inject
- Mitt Global event bus ;
One 、Provide and Inject( Descendants )
Provide/Inject Used to share data between non parent and child components :
- Such as the Some deeply nested components , The child component wants to get part of the content of the parent component ;
- under these circumstances , If we still take props Gradually pass along the component chain , It's going to be a lot of trouble ;
In this case , We can use Provide and Inject:
- No matter how deep the hierarchy is , The parent component can be used as the of all its child components Dependent provider ;
- The parent component has one provide Options To provide data
- The subcomponents are One inject Options To start using this data
Provide and Inject Use


In real development provide The data in it is not written dead , From the server , At this time, if you want to get data The data in it ,vue It is suggested that it is best to proviede Write it as a function .

Process responsive data :
Be careful : In the last case, our data is one-time , When our data changes ,Provide The data inside does not respond to changes ,(computed The arrow function is not bound this Of , That is to say, we now give length When you assign , Yes computed The return value of the function , because computed Inside this It's right data The value inside is dependent , Once the dependency changes ,computed The value of will be recalculated , There will be a new value , Will give you length Assign a new value .)

Use :

Two 、 Global event bus mitt library
1. Install first
npm install mitt -D2. secondly , We can encapsulate a tool eventbus.js:

3. Use

Monitoring events : Monitor in the life cycle , Also get emitter object

Mitt Event cancellation
// Cancel emitter All monitors in
emitter.all.clear()
// Define a function
function onFoo(){}
emitter.on('foo',onFoo) // monitor
emitter.off('foo',onFoo) // Cancel monitoring 边栏推荐
- toRefs API 与 toRef Api
- Tujia, muniao, meituan... Home stay summer war will start
- How does an enterprise manage data? Share the experience summary of four aspects of data governance
- Release notes of JMeter version 5.5
- OOM(内存溢出)造成原因及解决方案
- AVL树的实现
- Academic report series (VI) - autonomous driving on the journey to full autonomy
- Test of transform parameters of impdp
- Exception of DB2 getting table information: caused by: com ibm. db2.jcc. am. SqlException: [jcc][t4][1065][12306][4.25.13]
- Data of all class a scenic spots in China in 2022 (13604)
猜你喜欢

Abnova membrane protein lipoprotein technology and category display

DHCP路由器工作原理

Anr principle and Practice

Config distributed configuration center

服装门店如何盈利?

企業如何進行數據治理?分享數據治理4個方面的經驗總結

7天零基础能考证HCIA吗?华为认证系统学习路线分享

2018 Jiangsu Vocational College skills competition vocational group "information security management and evaluation" competition assignment

大咖云集|NextArch基金会云开发Meetup来啦

How can clothing stores make profits?
随机推荐
品牌·咨询标准化
ViewModelProvider. Of obsolete solution
Circulating tumor cells - here comes abnova's solution
Tujia, muniao, meituan... Home stay summer war will start
How to model and simulate the target robot [mathematical / control significance]
The startup of MySQL installed in RPM mode of Linux system failed
LC 面试题 02.07. 链表相交 & LC142. 环形链表II
IP address
请教一下,监听pgsql ,怎样可以监听多个schema和table
libcurl返回curlcode说明
sql中对集合进行非空校验
Big coffee gathering | nextarch foundation cloud development meetup is coming
Several index utilization of joint index ABC
$parent(获取父组件) 和 $root(获取根组件)
Basic introduction of JWT
详解机器翻译任务中的BLEU
freeswitch拨打分机号源代码跟踪
从零到一,教你搭建「CLIP 以文搜图」搜索服务(二):5 分钟实现原型
Config分布式配置中心
Test of transform parameters of impdp