当前位置:网站首页>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 边栏推荐
- 【mysqld】Can't create/write to file
- 从零到一,教你搭建「CLIP 以文搜图」搜索服务(二):5 分钟实现原型
- How Oracle backs up indexes
- 请教一个问题,flink oracle cdc,读取一个没有更新操作的表,隔十几秒就重复读取全量数据
- Esxi attaching mobile (Mechanical) hard disk detailed tutorial
- SQLMAP使用教程(四)实战技巧三之绕过防火墙
- JDBC database connection pool usage problem
- MySQL binlog related commands
- 7天零基础能考证HCIA吗?华为认证系统学习路线分享
- The latest trends of data asset management and data security at home and abroad
猜你喜欢

关于数据库数据转移的问题,求各位解答下

Several index utilization of joint index ABC

The latest trends of data asset management and data security at home and abroad

Big coffee gathering | nextarch foundation cloud development meetup is coming

非父子组件的通信

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

MATLAB小技巧(29)多项式拟合 plotfit

How can gyms improve their competitiveness?

Composition API 前提

Use of completable future
随机推荐
How to share the same storage among multiple kubernetes clusters
请教一下,监听pgsql ,怎样可以监听多个schema和table
SolidWorks的GB库(钢型材库,包括铝型材、铝管等结构)安装及使用教程(生成铝型材为例)
Initial experience of addresssanitizer Technology
带你刷(牛客网)C语言百题(第一天)
leetcode 509. Fibonacci number
How DHCP router works
From zero to one, I will teach you to build the "clip search by text" search service (2): 5 minutes to realize the prototype
Master-slave replication principle of MySQL
子组件传递给父组件
js小练习----分时提醒问候、表单密码显示隐藏效果、文本框焦点事件、关闭广告
Paranoid unqualified company
js小练习
服装门店如何盈利?
Several index utilization of joint index ABC
Tujia, muniao, meituan... Home stay summer war will start
云备份项目
Graduation design game mall
SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)
[Luogu p1971] rabbit and egg game (bipartite game)