V
V
Vitaly the Wise2018-03-11 16:16:00
Angular
Vitaly the Wise, 2018-03-11 16:16:00

How to know if a redirect went through in routerLink Angular 4?

Hello. I have a component that has a form. For this component, I set guard canDeactivate which blocks navigation to other pages if the form is not submitted. The problem is that I have a link to log out of my account:

<a routerLink="/login" (click)="logout($event)">Logout</a>

where
logout(event) {
  this.authService.logout();
}

The problem is that the application asks the user if he wants to leave the page, but also exits his profile. Is there any way to find out if the transition was made so that we can exit after it? Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Stroykin, 2018-03-12
@tapinambur0508

<button (click)="showConfirmLogout()">Logout</button>

public showConfirmLogout(): void {
  this._confirmService.open('Вы действительно хотите выйти?').then(() => {
    this._logout();
  });
}

private _logout(): void {
  this._authService.logout().subscribe(() => {
    this._router.navigate(['/login']);
  });
}

_confirmService is my service that replaces the default confirm. You can use something of your own or take ready-made or standard

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question