Vue3 global component communication provide/inject source code analysis
WBOY
Release: 2023-05-14 17:58:06
forward
1133 people have browsed it
1. Foreword
As the name suggests, the grandfather-grandson component has a deeper reference relationship than the communication between parent-child components (also called "generation-separated components"):
C component is introduced into In component B, component B is introduced into component A for rendering. At this time, A is the grandpa level of C (there may be more hierarchical relationships). If you use props, you can only pass them level by level, which is too cumbersome. , so we need a more direct method of communication.
The relationship between them is as follows. Grandson.vue is not directly mounted under Grandfather.vue. There is at least one Son.vue between them (there may be multiple):
Grandfather.vue
└─Son.vue
└─Grandson.vue
Copy after login
Because of the consistency of the relationship between superiors and subordinates, the scheme of grandfather-grandson component communication is also applicable to parent-child component communication. You only need to replace the grandfather-grandson relationship with a father-son relationship.
2. provide / inject
This feature has two parts: Grandfather.vue has a provide option to provide data, and Grandson.vue has an inject option to start using the data. .
Grandfather.vue passes value to Grandson.vue through provide (can include defined functions)
Grandson.vue passes value to Grandfather through inject .vue triggers the event execution of the grandpa component
No matter how deep the component hierarchy is, the component that initiates provide can be used as a dependency provider for all its subordinate components The content of this part The changes are very big, but it is actually very simple to use. Don't panic, there are also the same places:
The parent component does not need to know which child components use the property it provides
Subcomponents do not need to know where the inject property comes from
In addition, one thing to remember is that the provide and inject bindings are not responsive. This is intentional, but if a listenable object is passed in, the object's properties will still be responsive.
The old version of provide usage is similar to data, both are configured as a return object function. 3.x’s new version of provide is quite different in usage from 2.x.
In 3.x, provide needs to be imported and enabled in setup, and is now a completely new method. Every time you want to provide a piece of data, you must call it separately. Every time you call, you need to pass in 2 parameters:
The operation is very simple, right, but it should be noted that provide is not a response style, if you want to make it responsive, you need to pass in responsive data 4, receive inject Let’s first review the usage of 2.x:
The usage of the old version of inject is similar to that of props. The usage of the new version of 3.x inject is also quite different from that of 2.x. In 3.x, inject is the same as provide. It also needs to be imported first and then enabled in setup. It is also a brand new method.
Every time you want to inject a piece of data, you must call it separately. Every time you call, you only need to pass in 1 parameter:
基本数据类型,需要 provide 一个函数,将其 return 出去给子孙组件用,这样子孙组件每次拿到的数据才会是新的。 但由于不具备响应性,所以子孙组件每次都需要重新通过执行 inject 得到的函数才能拿到最新的数据。
按我个人习惯来说,使用起来挺别扭的,能不用就不用……
由于不具备真正的响应性,return 给模板使用依然不会更新视图,如果涉及到视图的数据,请依然使用 响应式 API 。
The above is the detailed content of Vue3 global component communication provide/inject source code analysis. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn