当前位置:网站首页>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 边栏推荐
- main函数在import语句中的特殊行为
- How can flinksql calculate the difference between a field before and after update when docking with CDC?
- ANR 原理及实践
- [noi simulation] regional division (conclusion, structure)
- Abnova circulating tumor DNA whole blood isolation, genomic DNA extraction and analysis
- MySQL binlog related commands
- 根据IP获取地市
- 什么情况下考虑分库分表
- Please ask a question, flick Oracle CDC, read a table without update operation, and repeatedly read the full amount of data every ten seconds
- Sqlserver multithreaded query problem
猜你喜欢

After the promotion, sales volume and flow are both. Is it really easy to relax?

Several index utilization of joint index ABC

Abnova circulating tumor DNA whole blood isolation, genomic DNA extraction and analysis

MOS tube parameters μ A method of Cox

$refs:组件中获取元素对象或者子组件实例:

MySQL的主从复制原理

Matlab tips (29) polynomial fitting plotfit

Four goals for the construction of intelligent safety risk management and control platform for hazardous chemical enterprises in Chemical Industry Park

Reflection (II)

Leetcode T1165: 日志分析
随机推荐
场馆怎么做体育培训?
健身房如何提高竞争力?
FPGA course: application scenario of jesd204b (dry goods sharing)
Tujia, muniao, meituan... Home stay summer war will start
How to do sports training in venues?
[Luogu p1971] rabbit and egg game (bipartite game)
main函数在import语句中的特殊行为
SQLMAP使用教程(四)实战技巧三之绕过防火墙
Stack Title: nesting depth of valid parentheses
组件的通信
毕业设计游戏商城
sql中对集合进行非空校验
Four goals for the construction of intelligent safety risk management and control platform for hazardous chemical enterprises in Chemical Industry Park
Implementation of AVL tree
Paranoid unqualified company
企業如何進行數據治理?分享數據治理4個方面的經驗總結
Maze games based on JS
RuntimeError: CUDA error: CUBLAS_ STATUS_ ALLOC_ Failed when calling `cublascreate (handle) `problem solving
The startup of MySQL installed in RPM mode of Linux system failed
Learning records on July 4, 2022