0
  • In my architecture, I'm using a routing main-component, to switch between sections of my app in which I use single components.

  • Also, I have one component in the main-component which stays in 'foreground' all the time and which is needed to be used in combination with the components of the other sections.

  • For communication between the 'fixed' component and the other components, I am using a subsciption to the topics of the fixed component.

  • Now: when switching through the sections via the routing module, the lifetime of the switchable components is over; their OnDestroy-Methods are getting called (the subscriptions get canceled.)

  • When an Event happens, while the other component is not created (which is the case when the user views another route), it can't receive the events.

  • In my use case it should be possible to trigger an event in the 'fixed' component, which then triggers the router to be switched to the correct routing-path and get the 'switch'-component-object. This object also needs the information, which the fixed component sent.

  • I hope, my explaination is thorough and understandable (also see the attached draft), I'm relatively new to angular and web programming in genereal .

  • Now I stumbled upon the Angular RouteReuseStrategy but I'm afraid it won't help, as it kind of 'recreates' a saved state of the before-visited route and the components behind it are 'inactive' and can't receive events either.

Does anyone has a hint for me? Maybe my concept needs a makeover.

enter image description here

Greets, Che

Che
  • 51
  • 7
  • 3
    Look into BehaviorSubject, or ReplaySubject. These types of subjects emit the last value (or the last _n_ values in the case of ReplaySubject) to new subscribers, so when you switch back to your switchable component, it will receive the data it needs by subscribing to the observables exposed by those subjects. – Octavian Mărculescu May 03 '22 at 11:03
  • Thanks for your comment. This lead me to the solution pretty straight forward. All I had to do was, to change my Subjects in the Service to BehaviorSubject. https://stackoverflow.com/questions/57355066/how-to-implement-behavior-subject-using-service-in-angular-8 – Che May 03 '22 at 14:03

0 Answers0