0

I want when someone press browser's back button to warn him and if he press ok then i will redirect to Home Component. I use CanDeactivate Guard and i want to change the nextState of CanDeactivateGuard. I tried to change the nextState :

nextState = { root: currentRoute, url: '/' }

but always redirect me to the previous component

can-deactivate-guard.service.ts

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { CanDeactivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';

export interface CanComponentDeactivate {
   canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}

 @Injectable({
  providedIn: 'root'
 })
 export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> {

 canDeactivate(component: CanComponentDeactivate,
 currentRoute: ActivatedRouteSnapshot,
 currentState: RouterStateSnapshot,
 nextState?: RouterStateSnapshot
 ): Observable<boolean> | Promise<boolean> | boolean {

 nextState = { root: currentRoute, url: '/' }// try this, but dosen't work
 return component.canDeactivate();
 }
}

My components code. pressContinue is just a boolean in case someone press continue button for the next component.

  canDeactivate() {
  if (this.pressContinue) {
     return true;
   } else {
     return confirm('Are you sure?');
   }
  }
Tzimpo
  • 964
  • 1
  • 9
  • 24
  • I change the logic on component code to and works: canDeactivate() { if (this.pressContinue) { return true; } else { var resp = confirm('Are you sure?'); if(resp == true) window.location.href = ''; return false; } } – Tzimpo Feb 25 '20 at 14:17

2 Answers2

1

Hope this helps...

app.service

 clickedBackButton = false;
 constructor() { }

 canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
     if(this.clickedBackButton) {
       this.clickedBackButton = false;
       return false;
     }
   return true;
  }

app.routing.module

const routes: Routes = [ 

  {path: '', component: HomeComponent, canActivate: [AppService]},
  {path: 'about', component: AboutComponent, canActivate: [AppService]}
];

app.component.ts

constructor(
private location: PlatformLocation,
private appService: AppService) {}

ngOnInit() {    
  this.location.onPopState(() => {
    let conf = confirm('Please confirm');
    if(conf) {
      this.appService.clickedBackButton = false;
      return;
    } 
    this.appService.clickedBackButton = true;
  });
}

app.component.html

<div class="container">
<div class="row">
    <div class="col-4">
        <button class="btn btn-success" style="margin-right: 5px;" [routerLink]="['/']" routerLinkActive="router-link-active" >Home</button>
        <button class="btn btn-primary" [routerLink]="['/about']" routerLinkActive="router-link-active" >About</button>
    </div>
</div>
<router-outlet></router-outlet>

Aish Anu
  • 1,552
  • 1
  • 8
  • 10
0

I don't know if it's the best solution but i change the logic on my component to redirect me to homepage and it's working!

canDeactivate() {
if (this.pressContinue) {
  return true;
 } else {
  var resp = confirm('Are you sure?');

  if(resp == true)
  window.location.href = '';

  return false;
 }
}
Tzimpo
  • 964
  • 1
  • 9
  • 24
  • routing by window.location.href does full page reload what will break your state and load a lot of redundant bytes – Eduard Void May 18 '21 at 13:13