0

In my Angular component, I have this

ngOnInit(): void {
this.route.paramMap
.pipe(
  tap(item=>console.log(item))
)
.subscribe(
  params => {
    const proyectoId = params.get('proyectoId');
    this.empleadoId = +params.get('empleadoId');
  }
  ,err=>console.log(err)
  ,()=>{
    console.log('Hola');
  }
);

}

But when I debug I don't see the console.log

log

Any idea, please?

Thanks

kintela
  • 1,283
  • 1
  • 14
  • 32

1 Answers1

1

The observable returned from Angular's ActivatedRoute#paramMap is perpetual unless closed explicitly. If you wish to use only the first emission (in most ideal cases it should be enough for paramMap), you could pipe in a take(1). It would close the observable after it's first emission.

this.route.paramMap.pipe(
  take(1)
  tap(item => console.log(item)),
).subscribe(
  ...
);

That said, if you couldn't use take(1), it's always good practice to close such observables in the component's ngOnDestroy() hook. It'd ensure there are no memory leaks from open observables after the component is closed. See here for more info.

ruth
  • 29,535
  • 4
  • 30
  • 57