First, let's talk about why we use provide/inject? For communication between grandfather and grandson components, or even between great-grandfather components and grandson components, we can just use vuex. That is indeed the case, but please listen to me, sometimes your project is relatively small and there are even few scenarios for component communication. In that case, isn’t it a waste to introduce vuex just for a few communication parameters? Some people may also think of using Without further ado, I will just tell you that using provide/inject is the solution to your problems. Let’s see how to use it? Backhand is just a few lines of simple code: 1. The parent component provides the parameters to be passed to the child component provide() { return { listType: this.listType, } } 2. Subcomponent usage: inject: ['listType'], Of course, you can also specify your default values and the source of your parameters in inject: inject:{ listType:{ from:"par"//provide the defined name default:1 } } Okay! Isn’t it simple? In fact, both parent components and ancestor components can inject dependencies into descendant components, no matter how deep the component hierarchy is. Some more: provide can be an object or a function that returns an object. inejct: can be a string array or an object. If you are interested, take a look at the source code below, which is also quite easy to understand: Provide the core source code: export function provide<T>(key: InjectionKey<T> | string | number, value: T) { if (!currentInstance) { if (__DEV__) { warn(`provide() can only be used inside setup().`) } } else { //Get the provides of the current component. The default instance inherits the provides object of the parent class. let provides = currentInstance.provides //Use the parent provided object as a prototype to create your own provided object const parentProvides = currentInstance.parent && currentInstance.parent.provides if (parentProvides === provides) { provides = currentInstance.provides = Object.create(parentProvides) } provides[key as string] = value } } Inject's core source code: export function inject( key: InjectionKey<any> | string, defaultValue?: unknown, treatDefaultAsFactory = false ) { //Get the current component instance const instance = currentInstance || currentRenderingInstance if (instance) { //Get provides const provides = instance.parent == null ? instance.vnode.appContext && instance.vnode.appContext.provides : instance.parent.provides if (provides && (key as string | symbol) in provides) { //If the key exists, return directly provides[key as string] } else if (arguments.length > 1) { //If the key does not exist, set the default value and return the default value directly return treatDefaultAsFactory && isFunction(defaultValue) ? defaultValue.call(instance.proxy) : defaultValue } else if (__DEV__) { //If none of them are found, warn(`injection "${String(key)}" not found.`) } } else if (__DEV__) { warn(`inject() can only be used inside setup() or functional components.`) } } This concludes this article on parsing the usage and principles of Vue's provide and inject. For more information on the usage of Vue provide and inject, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: HTML Frameset Example Code
>>: Example code for implementing a circular trajectory animation using CSS3 and table tags
Grammatical rules SELECT column_name(s) FROM tabl...
Preface The this pointer in JS has always been a ...
This article example shares the specific code of ...
This article uses examples to illustrate the usag...
Create a mysql user and authorize: Format: grant ...
But recently I found that using this method will c...
Step 1: yum install httpd -y #Install httpd servi...
You may have set a MySQL password just now, but f...
Table of contents Props comparison of class compo...
Table of contents 1. Overview 2. Name field 3. Ve...
What is Fuser Command? The fuser command is a ver...
This is my first blog. It’s about when I started ...
Here we only introduce the relatively simple inst...
This article example shares the specific code for...
1. Why create an index? (Advantages) This is beca...