variable filled in constructor is empty in NgOnInit

  angular, events, router, variables

I have two variables declared before constructor:

previousUrl: string = '';
link: string = '';

and filled in constructor:

  constructor(private router:Router) {
                this.router.events
                .pipe(filter((evt: any) => evt instanceof RoutesRecognized), pairwise())
                .subscribe((events: RoutesRecognized[]) => {
                  this.previousUrl = events[0].urlAfterRedirects;
                  if(this.previousUrl.indexOf('bla')){
                    this.link = "blabla";
                  } else if(this.previousUrl.indexOf('blubb')){
                    this.link = "blubbblubb";
                  } else {
                    this.link = "nothing";
                  }
                });
}

but when i make console.log on both in NgOnInit the output is empty…
when i make console.log within the constructor the outcome is correct and as expected.

I need to use both in my html – but in html they are empty too.

someone got an idea for me?

Source: Angular Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.