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 badges1 Answer
Reset to default 4Here are some differences that helped me choose in many situations
- props are reactive and provide / inject is not.
- 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.
- 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
版权声明:本文标题:javascript - Vue JS provideinject and Props - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744351475a2602075.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论