当前位置:网站首页>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
边栏推荐
- 多线程与高并发(9)——AQS其他同步组件(Semaphore、ReentrantReadWriteLock、Exchanger)
- 场馆怎么做体育培训?
- FPGA course: application scenario of jesd204b (dry goods sharing)
- Prime partner of Huawei machine test questions
- Graduation design game mall
- 2022/07/04学习记录
- Can 7-day zero foundation prove HCIA? Huawei certification system learning path sharing
- Sword finger offer high quality code
- MYSQL----导入导出&视图&索引&执行计划
- Abnova membrane protein lipoprotein technology and category display
猜你喜欢
从零到一,教你搭建「CLIP 以文搜图」搜索服务(二):5 分钟实现原型
健身房如何提高竞争力?
场馆怎么做体育培训?
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
Matlab tips (30) nonlinear fitting lsqcurefit
MATLAB小技巧(30)非线性拟合 lsqcurefit
After the promotion, sales volume and flow are both. Is it really easy to relax?
How can brand e-commerce grow against the trend? See the future here!
The latest trends of data asset management and data security at home and abroad
服装门店如何盈利?
随机推荐
JWT的基础介绍
. Net core accesses uncommon static file types (MIME types)
Jetpack Compose 远不止是一个UI框架这么简单~
Complete process of MySQL SQL
Config distributed configuration center
jdbc数据库连接池使用问题
Answer to the second stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
from .onnxruntime_pybind11_state import * # noqa ddddocr运行报错
异步组件和Suspense(真实开发中)
Special behavior of main function in import statement
毕业设计游戏商城
DHCP路由器工作原理
OOM(内存溢出)造成原因及解决方案
请教一下,监听pgsql ,怎样可以监听多个schema和table
How can brand e-commerce grow against the trend? See the future here!
多个kubernetes集群如何实现共享同一个存储
libcurl返回curlcode说明
How does an enterprise manage data? Share the experience summary of four aspects of data governance
联合索引ABC的几种索引利用情况
Precise space-time travel flow regulation system - ultra-high precision positioning system based on UWB