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 aattribute:inject. Then I checked the official website provider/inject
provider/inject: Simply put, 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 } }
<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>
<template> <p> {{demo}} </p> </template> <script> export default { name: "", inject: ['for'], data() { return { demo: this.for } } } </script>
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!