3

I can't get route params data in modal components.

Url: http://localhost:4200/portal/profiles/edit/5303

routing:

{ 
  path: ':accountId', 
  component: ProfileOverviewComponent, 
  data: { 
    title: 'Profile Overview', 
    icon: 'fa fa-id-card-o' 
  }
},

This command returns false:

if (this.route.snapshot.paramMap.get('accountId'))
Wilt
  • 41,477
  • 12
  • 152
  • 203
Bora Açıcı
  • 31
  • 1
  • 3

1 Answers1

2

Modal components created with a named outlet are independent of the (unnamed) primary route but simply stacked on top. Would you need to get the route params (or any other data) from the primary route you can access it through the "root" property of the activated route.

You can do that like this:

const getLastChild = (route) => {
  let child = route;
  while (child.firstChild) {
    child = child.firstChild
  }
  return child;
}

const primary = this.route.snapshot.root; // Get the primary route
const lastChild = getLastChild(primary); // Get the last child (from which you want the params).
const params = lastChild.params;

This should resolve your problem, otherwise leave a comment.

Wilt
  • 41,477
  • 12
  • 152
  • 203