admin管理员组

文章数量:1402350

When should provide/inject be used pared to props.

In my opinion props make ponent more meaningful.

Provide/inject makes ponents tightly coupled.

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

Please advice

Thanks

When should provide/inject be used pared to props.

In my opinion props make ponent more meaningful.

Provide/inject makes ponents tightly coupled.

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

Please advice

Thanks

Share Improve this question asked Aug 12, 2019 at 20:50 RaviRavi 2,5683 gold badges27 silver badges32 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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 ponents.

From docs:

Provide / inject are used together to allow an ancestor ponent to serve as a dependency injector for all its descendants, regardless of how deep the ponent 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

本文标签: javascriptVue JS provideinject and PropsStack Overflow