Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
56
rated 0 times [  58] [ 2]  / answers: 1 / hits: 6602  / 5 Years ago, mon, august 12, 2019, 12:00:00

When should provide/inject be used compared to props.



In my opinion props make component more meaningful.



Provide/inject makes components tightly coupled.



What are the use cases where using provide/inject is a better approach.



Please advice



Thanks


More From » vue.js

 Answers
1

Here are some differences that helped me choose in many situations




  1. props are reactive and provide / inject is not.

  2. props can only be used on direct child components.



From docs:




Provide / inject are used together to allow an ancestor component to serve as a dependency injector for all its descendants, regardless of how deep the component hierarchy is, as long as they are in the same parent chain. If you are familiar with React, this is very similar to React’s context feature.





  1. You can use provide / inject to set a default value for props.



example:



const Child = {
inject: ['foo'],
props: {
bar: {
default () {
return this.foo
}
}
}
}


Here is an article of a good example when to use provide / inject


[#6607] Friday, August 9, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
clarkulisesa

Total Points: 422
Total Questions: 93
Total Answers: 112

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
clarkulisesa questions
;