Home  >  Article  >  Web Front-end  >  Detailed explanation of the steps to use provide/inject in vue

Detailed explanation of the steps to use provide/inject in vue

php中世界最好的语言
php中世界最好的语言Original
2018-06-01 10:38:013446browse

This time I will bring you a detailed explanation of the steps for using provide/inject in vue. What are the precautions for 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


Third Define another subcomponent


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

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:

How to use vue to implement the 2048 mini game

How to use vee-validate in Vue

The above is the detailed content of Detailed explanation of the steps to use provide/inject in vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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