Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.1K
Views
El enrutador getCurrentNavigation siempre devuelve nulo

En la última versión de Angular 7.2.6, estoy tratando de pasar datos en el enrutador

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

En el archivo OtherComponent , this.router.getCurrentNavigation() siempre devuelve nulo

Enlace de Stackblitz

Documentos angulares - getCurrentNavigation

Documentos angulares - estado

over 4 years ago · Santiago Trujillo
5 answers
Answer question

0

cambie el código de esta manera porque después de constructor() solo se llama a ngOnInit() , por lo que el valor se vuelve nulo

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

0

Está llamando al método getCurrentNavigation demasiado tarde. La navegación ha terminado.

Debe llamar al método getCurrentNavigation dentro del constructor:

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

O si desea acceder a la navegación en ngOnInit , puede hacer lo siguiente:

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

0

trabaja para mi iónico 4

  • this.router.url => /app/menu/etc // posición actual
over 4 years ago · Santiago Trujillo Report

0

Esto me pasó porque había una promesa (proceso asíncrono) en el Evento de NavigationEnd, antes de consumir getCurrentNavigation().extras().state del estado actual.

Moví la promesa después y ¡voilá! Se las arregló para obtener los datos sin ser limpiado primero.

over 4 years ago · Santiago Trujillo Report

0

Para especificaciones (pruebas), getCurrentNavigation() en el constructor no funciona, debe usar el history en ngOnInit() y simular valores en el history , por ejemplo:

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

Ahora su componente puede usar estos valores en ngOnInit () y usarlo según las especificaciones:

 ngOnInit(): void { console.log(history.state.name); }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!