-1

My routes with data.

    const routes: Routes = [
    { path: '', component: HomeComponent, pathMatch: 'full' },
    { path: 'login', component: LoginComponent },
    { path: 'customer', component:  CustomerComponent, canActivate: [AuthGuard], data: { title: 'Customer' }, children: [
        { path: 'new', component:  CreateCustomerComponent, data: { title: 'New customer' }, canActivate: [AuthGuard]}
    ]}
];

Get data from active route in component:

this.activeComponent = this.route.snapshot.data.title;
if ( this.route.snapshot.firstChild) {
  this.activeComponent = this.route.snapshot.firstChild.data.title;
}

I can't get that child's data title.

Ivo Oostwegel
  • 374
  • 2
  • 20

1 Answers1

0

First of all, at the moment of getting snapshot, the child is not rendered yet. As a result, you don't have child's data. You may use @ViewChild and then get child's data or create service to communicate. You may also subscribe to route's changes (ActivatedRoute#data)

be4code
  • 688
  • 4
  • 15