Maik Lowrey
Maik Lowrey

Reputation: 17556

When to use use Provide / Inject and when config.globalProperties

I'm currently working on a small VueJs SPA and the first time with VueJs 3. In the past you could add helpers, classes to the Vue instance simply by using prototype. With VueJs 3 this possibility was abolished and replaced with two new possibilities:

  1. provide / inject
  2. config.globalProperties

I personally find variant 2 charming because it is easy and quick to implement. With variant one, I don't really understand the concept. I wonder why you explicitly add the inject in the child componente and why it doesn't happen automatically. However.

Are there different application scenarios for using 1 and 2? I only ask because I find it strange to add helper classes under config. So weird from the naming.

Upvotes: 0

Views: 749

Answers (1)

Bas Wagenmaker
Bas Wagenmaker

Reputation: 223

With provide / inject you can pass data from a parent component to a deeper nested child component, without the use of prop drilling.

Using config.globalProperties would mean that the data is available in all the components, instead of just the components that require it.

Upvotes: 1

Related Questions