kintela
kintela

Reputation: 1323

nothing happens when the observable is completed in my angular component

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

Upvotes: 0

Views: 219

Answers (1)

Barremian
Barremian

Reputation: 31125

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.

Upvotes: 1

Related Questions