当前位置:网站首页>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 -D
2. 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
边栏推荐
- 子组件传递给父组件
- 带你刷(牛客网)C语言百题(第一天)
- 请教一下,监听pgsql ,怎样可以监听多个schema和table
- The startup of MySQL installed in RPM mode of Linux system failed
- SQLMAP使用教程(四)实战技巧三之绕过防火墙
- 【NOI模拟赛】区域划分(结论,构造)
- Esxi attaching mobile (Mechanical) hard disk detailed tutorial
- Special behavior of main function in import statement
- 7天零基础能考证HCIA吗?华为认证系统学习路线分享
- DHCP路由器工作原理
猜你喜欢
2022年全国所有A级景区数据(13604条)
SQLMAP使用教程(四)实战技巧三之绕过防火墙
Mysql---- import and export & View & Index & execution plan
How can clothing stores make profits?
Basic introduction of JWT
多个kubernetes集群如何实现共享同一个存储
Abnova membrane protein lipoprotein technology and category display
MOS tube parameters μ A method of Cox
Release notes of JMeter version 5.5
品牌·咨询标准化
随机推荐
FPGA course: application scenario of jesd204b (dry goods sharing)
Esxi attaching mobile (Mechanical) hard disk detailed tutorial
Please answer the questions about database data transfer
linux系统rpm方式安装的mysql启动失败
Answer to the first stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
How to model and simulate the target robot [mathematical / control significance]
After the promotion, sales volume and flow are both. Is it really easy to relax?
联合索引ABC的几种索引利用情况
Advantages of using net core / why
多线程与高并发(9)——AQS其他同步组件(Semaphore、ReentrantReadWriteLock、Exchanger)
组件的嵌套和拆分
Brand · consultation standardization
Implementation of AVL tree
Please ask a question, flick Oracle CDC, read a table without update operation, and repeatedly read the full amount of data every ten seconds
MYSQL----导入导出&视图&索引&执行计划
toRefs API 与 toRef Api
请教一下,监听pgsql ,怎样可以监听多个schema和table
Several index utilization of joint index ABC
Bus消息总线
How can gyms improve their competitiveness?