0

I have a List and Details components in an application and I am trying to navigate to Details component by passing id parameter. However, there is not a reponse or error when calling the following method. I also share the routing.module:

routing.module

const routes: Routes = [
  {
    path: '',
    component: ListComponent,
    data: {...}
  },
  {
    path: '/details/:id',
    component: DetailsComponent,
    data: {...}
  }
];

list.component

constructor(private router: Router) {}

details(id) {
  // the code hits here and get the id parameter correctly
  this.router.navigate(['/details'], {
    queryParams: { id: id }
  });
}

details.component

constructor(private route: ActivatedRoute) { }

ngOnInit(): void {
  this.route.paramMap
    .subscribe(params => {
      let id = +params.get('id');
    });
}

So, what is wrong with this approach? The ngOnInit block of the details page is not fired.

4
  • 1
    this.router.navigate(['/details/'+id]) Commented Dec 17, 2020 at 7:52
  • Thanks, voted up. What about passing this received id value to the child component (that is called via ``<router-outlet></router-outlet>`)? Commented Dec 18, 2020 at 9:08
  • an element into <router-outlet> is not a children, if you has a children use @Input():angular.io/guide/… Commented Dec 18, 2020 at 9:51
  • Yes, what about passing data to a component in <router-outlet></router-outlet>? Commented Dec 18, 2020 at 21:23

3 Answers 3

2

In your route, you have specified '/details/:id' where ID is Router Param not a Query Param.

Thus, if you want to navigate to that url, use this instead:

ListComponent

this.router.navigate(['/details', id])

DetailsComponent

constructor(private route: ActivatedRoute) {}


ngOnInit() {
   const id = this.route.snapshot.params.id;      // Fetch the ID from your
                                                  // current route "/details/:id"
}

or you can also do it this way

ngOnInit() {
  this.route.params.subscribe(params => console.log(params.id))
}

More info on Angular Router Documentation

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it seems to work, voted up. What about passing this received id value to a component in <router-outlet></router-outlet> ?
I'm quite not sure if I follow, but I have updated my answer based on what I have understood from your question
1

you have to add queryParamsHandling: 'merge' to your code

details(id) {
  // the code hits here and get the id parameter correctly
  this.router.navigate(['/details'], {
  queryParams: { id: id },
  queryParamsHandling: 'merge'
  });
}

Comments

0

Hello in the app component.html

Please add

<router-outlet></router-outlet>

1 Comment

What about passing this received id value to a component in <router-outlet></router-outlet> ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.