0

I read a router param in my MainComponent in this way:

  ngOnInit(){
      this.activatedRoute.params.subscribe(params => {
         const id = params['id'];
         console.log(id);
     });
 }  

I need to read "id" in another component (ContactComponent) that is no related to MainComponent, here's my routing:

const routes: Routes = [
{ path: 'contatti', component: ContattiComponent },
{ path: 'main/:id', component: MainComponent}
  ];

my idea was to store "id" in a global variable and then read in ContactComponent that global variable. I have no idea on how to do so.

Francesco
  • 51
  • 2
  • 8
  • 1
    Service is one of the way to store the value in one place and access it globally. – Chellappan வ Oct 09 '19 at 05:07
  • check this:https://stackoverflow.com/questions/44414226/angular-4-pass-data-between-2-not-related-components – Chellappan வ Oct 09 '19 at 05:08
  • Possible duplicate of [passing data from one typescript file to another in angular](https://stackoverflow.com/questions/57956854/passing-data-from-one-typescript-file-to-another-in-angular) – Yash Oct 09 '19 at 05:16
  • @Francesco what's your use case? If you need to access the `id` param from the URL, you probably should have it in there. If you open `ContattiComponent` directly, without opening the `MainComponent`, there would be no `id`, regardless of how you store it – Pawel Janicki Oct 09 '19 at 06:15

0 Answers0