vue3/pinia:将主存储添加到每个组件:mainstore()还是提供/注入?
将PINIA存储添加到VUE3组件的默认方法是通过在每个VUE3组件中加载namestore.js
import {nameStore} from "@stores/nameStore.js"
const nameStr = nameStore()
。
几乎所有主要组件中使用的主要PINIA商店的方法都更好? (但在小部分中使用不多)
Default way to add a pinia store to Vue3 component is through loading nameStore.js in each Vue3 component
import {nameStore} from "@stores/nameStore.js"
const nameStr = nameStore()
Vue3 has it's own Provide/Inject.
Which approach is better for the main pinia store that's used in almost all major components? (but not much used in small components)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在功能上,您的两个选项之间没有不同的不同,如@duannx所述。归结为偏好。
使用提供/注入时,请避免导入语句和函数调用,将样板还原为单行
const namestore = inmext('namestore')
,这些缺点是带有supply/supply/suppory/suffor/注入:注入
呼叫将返回undefined
,只能在运行时找到。IMO导入和调用商店挂钩的“正常”方式没有类似的问题,因此我通常建议使用它而不是提供/注入,这增加了(再次是IMO)不必要的复杂性。但是正如我所说,最终这是一个品味问题:)
Functionally there is no differece between your two options, as already stated by @Duannx. It comes down to preferences.
When using provide/inject you avoid the import statement and the function call, reducing the boilerplate to a single line
const nameStore = inject('nameStore')
, the drawbacks are the usual ones that come with provide/inject:inject
call will returnundefined
, which will only be found out during runtime.IMO the "normal" way of importing and calling the store hook has no similar problems, so I would generally suggest to use it rather than provide/inject, which adds (again, IMO) unnecessary complexity. But as I said, ultimately it's a matter of taste :)