Home Web Front-end JS Tutorial Use case analysis of provide/inject in vue

Use case analysis of provide/inject in vue

May 22, 2018 pm 02:55 PM
inject provide case analysis

This time I will bring you a use case analysis of provide/inject in vue. What are the precautions when using provide/inject in vue. The following is a practical case, let's take a look.

Preface

I was looking at the source code of element-ui recently and found such a attribute:inject. Then I checked the official website provider/inject

provider/inject: Simply put, variables are provided through provider in the parent component, and then injected into the child component through inject.

It should be noted that no matter how deep the subcomponent is, as long as inject is called, the data in the provider can be injected. Instead of being limited to getting data only from the prop attribute of the current parent component.

Let’s verify our conjecture:

First: Define a parent component

<template>
 <p>
<childOne></childOne>
 </p>
</template>
<script>
 import childOne from '../components/test/ChildOne'
 export default {
  name: "Parent",
  provide: {
   for: "demo"
  },
  components:{
   childOne
  }
 }
Copy after login

Here we provide for this variable in the parent component.

Second Define a subcomponent

<template>
 <p>
  {{demo}}
  <childtwo></childtwo>
 </p>
</template>
<script>
 import childtwo from './ChildTwo'
 export default {
  name: "childOne",
  inject: ['for'],
  data() {
   return {
    demo: this.for
   }
  },
  components: {
   childtwo
  }
 }
</script>
Copy after login

Third Define another subcomponent

<template>
 <p>
  {{demo}}
 </p>
</template>
<script>
 export default {
  name: "",
  inject: ['for'],
  data() {
   return {
    demo: this.for
   }
  }
 }
</script>
Copy after login

In the 2 subcomponents we use jnject to inject the variable for provided by provide and add it Provided to the data attribute.

The official website here indicates that the example only works in Vue 2.2.1 or higher. Below this version, the injected value will be obtained after props and data are initialized.

Check the results after running

As can be seen from the above example, as long as it is called in the parent component, it will take effect in the parent componentLife cycle, all child components can call inject to inject the value in the parent component.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps to add, delete and modify JavaScript DOM elements

Detailed explanation of the steps for Vue to use vee-validate to verify the form

The above is the detailed content of Use case analysis of provide/inject in vue. For more information, please follow other related articles on the PHP Chinese website!

Statement of this 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use provide/inject in Vue to implement method transfer between ancestor components and descendant components How to use provide/inject in Vue to implement method transfer between ancestor components and descendant components Jun 11, 2023 pm 12:17 PM

As a popular front-end framework, Vue provides a variety of methods to organize and manage interactions between components. In Vue, provide and inject are two methods that can be used to implement method transfer between ancestor components and descendant components. Provide and inject are methods for communication between advanced components provided by Vue. Their function is to provide data to ancestor components, and then use the inject method to receive data in descendant components. 1. Definition of provide and inject pro

How to use provide and inject in Vue3 How to use provide and inject in Vue3 May 11, 2023 pm 11:52 PM

1. Scene reproduction Don’t worry about the meaning of the title API. Here I will start by writing a more common scenario. The internal code of the corresponding component is relatively simple, and I will not show it here. Logically, these three components are referenced layer by layer. The corresponding page effect is as follows: As shown above, this is a very common scenario in projects, with three layers of nested components. (In fact, there are still deep levels of nesting. For now, it is enough for us to take three levels of nesting as an example.) OK, your current requirement is: you need to provide a string data "Han Zhenfang" in the grandfather component to provide it to the son component. If you are smart, you must have thought of props. Without further ado, let’s get started. 2. Passing Props "How advanced I thought it was. Isn't this the scene where data is passed from father to son?

Use provide and inject in Vue to implement data transfer and performance optimization between components Use provide and inject in Vue to implement data transfer and performance optimization between components Jul 17, 2023 pm 07:19 PM

Use provide and inject in Vue to achieve data transfer and performance optimization between components. In Vue, data transfer between components is a very common requirement. Sometimes we want to provide data at a node in the component tree and then use the data in its descendant components. In this case, we can use Vue's provide and inject to achieve this. In addition to data transfer, provide and inject can also be used for performance optimization, reducing the level of props transfer and improving component performance. prov

How to use provide/inject in Vue to transfer data between ancestor components and descendant components How to use provide/inject in Vue to transfer data between ancestor components and descendant components Jun 11, 2023 am 11:36 AM

In Vue, we often need to transfer data between components. When passing data between ancestor components and descendant components, we can use provide/inject provided by Vue. 1. The role of provide/inject In Vue, provide and inject are a pair of APIs used for data transfer between ancestors and descendants. Specifically, provide is used to define some data/methods that need to be shared in ancestor components, while inject is used to

Vue error: Unable to use provide and inject correctly for dependency injection, how to solve it? Vue error: Unable to use provide and inject correctly for dependency injection, how to solve it? Aug 25, 2023 pm 10:13 PM

Vue error: Unable to use provide and inject correctly for dependency injection, how to solve it? During the development process of Vue, we often need to share data or methods between components. Vue provides multiple ways to implement component communication, one of which is dependency injection through provide and inject. However, when using provide and inject for dependency injection, sometimes we encounter errors. This article will discuss solutions to these problems. error message when we are

Practical tips and case studies for data type conversion using numpy Practical tips and case studies for data type conversion using numpy Jan 26, 2024 am 08:21 AM

Practical skills and case analysis of numpy data type conversion Introduction: In the process of data analysis and scientific computing, it is often necessary to type conversion of data to adapt to different computing needs. As a commonly used scientific computing library in Python, numpy provides a wealth of data type conversion functions and methods. This article will introduce the practical skills of data type conversion in numpy and demonstrate its specific application through case analysis. 1. Background and significance of data type conversion When performing data analysis and scientific calculations, different types of data may require

What is provide & inject in Vue and how to use it? What is provide & inject in Vue and how to use it? Jun 11, 2023 pm 12:05 PM

Vue.js is a very popular JavaScript framework in the current front-end world. It has many excellent features such as responsive data binding, componentized view architecture, dependency tracking, and template rendering. The most commonly used function is component programming. Vue provides us with functional tests such as component registration, component parameter passing, etc. However, in some cases, the transmission of component data will encounter more difficult problems. At this time , we can use provide and inj provided in Vue

Vue3 global component communication provide/inject source code analysis Vue3 global component communication provide/inject source code analysis May 14, 2023 pm 05:58 PM

1. Preface As the name suggests, the grandfather-grandchild component has a deeper reference relationship than the communication between parent-child components (also called "generation-separated components"): C component is introduced into B component, and B component is introduced into A component 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 communication method. 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 because of up and down

See all articles