Skip to content

@angular/router: ActivatedRoute.snapshot.title is always undefined #47459

@markostanimirovic

Description

@markostanimirovic

Which @angular/* package(s) are the source of the bug?

router

Is this a regression?

No

Description

When the title is added as follows:

{
  path: '',
  component: HelloComponent,
  title: 'HELLO',
}

and we try to access it via route.snapshot.title:

export class HelloComponent implements OnInit {
 constructor(private readonly route: ActivatedRoute) {}

 ngOnInit(): void {
   console.log(this.route.snapshot.title); // undefined
 }
}

it will be undefined. However, we can still get the route title from route.snapshot.data:

const routeTitleKey = Object
  .getOwnPropertySymbols(this.route.snapshot.data)
  .find((symbol) => symbol.toString() === 'Symbol(RouteTitle)');

console.log(this.route.snapshot.data[routeTitleKey]); // HELLO

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-hqjky1?file=src%2Fapp%2Fhello.component.ts

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular 14.2.2

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions