Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

angular - Passing data between components without @Input/@Output

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...