简体   繁体   English

如何使用ts将数据传递给vue3中的多深度子组件

[英]How to pass data to multi-depth child component in vue3 with ts

I try to pass data from parent component to its multi-depth child component and only its deepest component need this data.我尝试将数据从父组件传递到其多深度子组件,并且只有其最深的组件才需要此数据。 Now I try to use emit and props in every child component to pass data.Is there a more elegant way to achieve it?现在我尝试在每个子组件中使用emit和props来传递数据。有没有更优雅的方法来实现它? Thanks in advance.提前致谢。

This sounds like a perfect use case for vuex.这听起来像是 vuex 的完美用例。 If you want to prevent prop or event drilling all the way up and down to the most deeply nested child components, then you should think about creating this portion of your app state within a global store.如果您想防止 prop 或事件一直钻到嵌套最深的子组件,那么您应该考虑在全局商店中创建应用程序 state 的这一部分。

Then you can simply just call a mutation, change the value from whatever component you want to change it via a mutation and the parent component will know about the changed state.然后你可以简单地调用一个突变,从你想通过突变改变它的任何组件更改值,父组件将知道更改的 state。

Depending on if you are using the Options or the Composition API, the code will look slightly different.根据您使用的是选项还是组合 API,代码看起来会略有不同。

Because you are using typescript, you should maybe think about using the Composition API because it works better for typescript projects.因为您使用的是 typescript,所以您可能应该考虑使用组合 API,因为它更适用于 typescript 项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM