Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
Router getCurrentNavigation always returns null

In the latest version of Angular 7.2.6, I'm trying to pass data in router itself

this.router.navigate(['other'], {state: {someData: 'qwert'}}

In the OtherComponent file, this.router.getCurrentNavigation() always return null

Stackblitz Link

Angular Docs - getCurrentNavigation

Angular Docs - state

over 4 years ago · Santiago Trujillo
5 Respuestas
Responde la pregunta

0

change the code like this because after constructor() only the ngOnInit() gets called so the value is getting null

constructor(private router: Router) {
   this.name = this.router.getCurrentNavigation().extras.state.example;
}
over 4 years ago · Santiago Trujillo Denunciar

0

You're calling the method getCurrentNavigation too late. The navigation has finished.

You need call the getCurrentNavigation method inside of the constructor:

constructor(private router: Router) {
    this.name = this.router.getCurrentNavigation().extras.state.someData;
}

Or if you want to access the navigation in ngOnInit you can do following:

ngOnInit() {
    this.name = history.state.someData;
}
over 4 years ago · Santiago Trujillo Denunciar

0

work for me Ionic 4

  • this.router.url => /app/menu/etc // current position
over 4 years ago · Santiago Trujillo Denunciar

0

This happened to me because there was a promise (asynchronous process) in the Event of the NavigationEnd, before consuming the getCurrentNavigation().extras().state of the current state.

I moved the promise after and voila! It managed to get the data without first being cleaned.

over 4 years ago · Santiago Trujillo Denunciar

0

For spec (tests), the getCurrentNavigation() on constructor don´t work, you have to use the history at ngOnInit() and mock values at history, for example:

beforeEach(() => {
    window.history.pushState({name: 'MyName', id: 1}, '', '');
}

Now your component can use this values on ngOnInit() and use on spec.:

ngOnInit(): void {
    console.log(history.state.name);
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda