在没有@ Input/@ Output的组件之间传递数据 [英] Passing data between components without @Input/@Output

查看:84
本文介绍了在没有@ Input/@ Output的组件之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我需要一个购物车",例如从一个组件到站点其余部分的关系.我之所以说喜欢的购物车",是因为它基本上是用户浏览时产生的需求的列表.它没有价格或税收逻辑,但是每种类型的需求都有自己的sub-parameters,这与我正在为其创建interfaces的其他需求不同.我认为needs component将使用any类型来接受传入的数据块.

从现在起,通过整棵树创建@Inputs@Outputs的庞大公路,只是要越过一个分支然后再向下走一层,似乎太繁琐了.我认为,如果我可以创建一个直接推送到needs component的函数,或者创建一个类将所有内容推送到needs component并对其进行预订,那会很好.

我将搜索条件设置为2月1日-现在,找到了这个 使用Angular2中的服务在组件之间共享数据

它进一步揭示了可能的事实,但是由于我不知道该怎么做,所以我不知道如何从中得出答案.

我没有提供任何尝试代码,因为我不知道要尝试什么,所以我所拥有的只是我的组件.看到我在问一种创建关系的方法,无论关系落在组件树上的什么地方,我都没有看到发布基本上只说<h1>hi</h1>来表明它们正在工作的组件的意义.如果您仍然想查看我的代码,我会发布它,但是它并不多.我仍在使用Angular2,目前无法将Node和NPM更新为使用4.任何有关如何实现这一目标的信息将不胜感激.

解决方案

利用Angular Services,它们是在组件之间传递数据的一种,并且由于输入和输出很容易,您将陷入事件发射器的意粉中,而且您可以查看ngrx存储以将数据保存在一个地方.但我建议您仅在中大型应用程序中使用ngrx,因为它会增加一些代码.

我的仓库详细使用了这两个概念 https://github.com/rahulrsingh09/AngularConcepts

In a nutshell, I need a "shopping cart" like relationship from one component to the rest of the site. I say "shopping cart like" because it's basically a list of needs generated by the user as they browse. It won't have any prices or tax logic, but each type of need will have its' own sub-parameters unique from the others which I'm in the midst of creating interfaces for. I figured the needs component would use types of any to accept the incoming chunks of data.

As of right now creating massive highways of @Inputs and @Outputs through the entire tree just to go over one branch and back down a tier seems excessively tedious. I figured it would be good if I could create a function that pushes directly to the needs component or maybe create a class to push everything to and make a subscription to it in the needs component.

I set a filter on my search to February 1st - now and found this Share data between components using a service in Angular2

It sheds a little more light on the fact that it's possible, but as I don't know how I should go about this I don't know how to derive an answer out of it.

I haven't come up with any code for attempting it because I don't know what to attempt so all I have is my components. Seeing that I'm asking about a way to create a relationship that works no matter where things fall on the component tree I don't see any point in posting components that basically only say <h1>hi</h1> so far to show they're working. If you'd still like to see my code I'll post it, but it isn't much lol. I'm still using Angular2 and can't update Node and NPM to use 4 at the moment. Any info on how to achieve this would be greatly appreciated.

解决方案

Make use of Angular Services they are one of passing data between components and also easy as Input and Output you will get stuck in a Spaghetti of event emitters , also you can look at the ngrx store for holding data in one place. but i would suggest you to use ngrx only in case of medium to large scale apps as it adds up a bit of code.

My repo that has used both these concepts in details https://github.com/rahulrsingh09/AngularConcepts

这篇关于在没有@ Input/@ Output的组件之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆