0

I have one component (Component A - All books) that triggers a route to another component (Component B - Selected Book) on a click event like so:

selectedItem(details){
    this.router.navigate(['/book', details.id]);
  }

What I'm trying to accomplish is pass the selected book details to Component B when the click event in Component A is executed.

I know we can use the Input() decorator to pass data from Parent to Child component in Angular. But that wouldn't work here since I'm not explicitly writing the Component B inside A like: <component-b></component-b>

In my case should I use a shared service to pass data around?

I'd also like to know some examples that can be considered as a parent-child relationship. In this book example, can books and selected book be considered a parent/child relationship? I've made some small Angular projects and I've always struggled with the concept.

blankface
  • 5,757
  • 19
  • 67
  • 114
  • https://stackoverflow.com/questions/31026886/how-do-i-share-data-between-components-in-angular-2 – AT82 Oct 28 '17 at 12:48
  • 1
    Service would be the way to go :) input/output does not work unless the child tag is in the parent like you point out yourself. – AT82 Oct 28 '17 at 12:50
  • Thank you. One further question... how do I persist the data in Component B after page refresh? – blankface Oct 28 '17 at 14:29

0 Answers0