How to limit subscribe in just one loop

0

I need to display an alert every time my main component changes route, so I created the following script in my AppComponent :

  constructor(private router: Router) {
      router.events.subscribe((val) => {
      alert(val);
    });
 }

But it displays the alert several times, but it displays the first correctly, how can I display only the first value of the subscribe, and ignore the rest? giving the alert just once inside the subscribe

    
asked by anonymous 20.11.2018 / 20:24

1 answer

1

You can use the take operator

link

  router.events.pipe(take(1)).subscribe((val) => {
      alert(val);
    });
    
20.11.2018 / 21:31