1

So I want to show a confirmation popup when user tries t navigate to other page. I read about hostListner and canActivate ...But I'm not getting any idea where to start!

please help !!TIA.

Priyanka
  • 370
  • 1
  • 6
  • 15

1 Answers1

2

first of all you need to create a guard for routing create a file named can-exit.guard.ts


import {Injectable} from '@angular/core';
import {CanDeactivate} from '@angular/router';

import {Observable} from 'rxjs';

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

@Injectable()
export class CanExitGuard implements CanDeactivate<CanExit> {
  canDeactivate(component: CanExit) {
    if (component.canDeactivate) {
      return component.canDeactivate();
    }
    return true;
  }
}

create another file canExit.ts – This file is used to return the promised value.

import {Observable} from 'rxjs';

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

After you add the above files in your application configure the router file as follows

const routes: Routes = [
  {
    path: 'your Path name',
    component: YourComponent ,
    canDeactivate: [CanExitGuard],
  }
];
// use this configuration in every path  of your application

Your component file needs some changes include canactivate in your component


import { CanExit } from './../can-exit.guard';  //important


@Component({
  // your selector and component defs
})
export class YourComponent implements OnInit, CanExit {

  constructor() { }

  ngOnInit() {
  }

  canDeactivate(): Promise<any> | boolean {
    const confirmResult = confirm('Are you sure you want to leave this page ? ');
    if (confirmResult === true) {
      return true;
    } else {
      return false;
    }
  }

}

hope this will help

Dewanshu
  • 532
  • 2
  • 16
  • thanks for help i was trying to do the same thing and you explained it very well – User209831 Jul 02 '20 at 10:01
  • I tried its throwing error TypeError: Cannot read property 'canDeactivate' of null !! @Dewanshu – Priyanka Jul 02 '20 at 10:16
  • Please check your route, and make sure, that you have set the component property. can you share your code – Dewanshu Jul 02 '20 at 10:20
  • @User209831 did u try this ?? is it working fine for u ? – Priyanka Jul 02 '20 at 10:20
  • `canDeactivate(MobilityStepComponent: CanExit) { if (MobilityStepComponent.canDeactivate) { return MobilityStepComponent.canDeactivate(); } return true; }` Here I'm getting MobilityStepComponent as null – Priyanka Jul 02 '20 at 10:23
  • @Dewanshu rest everything I replicated the same ! – Priyanka Jul 02 '20 at 10:26
  • are you reaching till popup or this error is coming before that – Dewanshu Jul 02 '20 at 10:28
  • popup is also not coming !! this problem is cause I've lazy loading implemented in my application! – Priyanka Jul 02 '20 at 10:30
  • Ok i find this problem i think its exactly what you are doing please have alook and let me know https://stackoverflow.com/questions/51450323/typeerror-cannot-read-property-candeactivate-of-null – Dewanshu Jul 02 '20 at 10:42
  • @Dewanshu Even I tried the same its working fine !! thank you so much – Priyanka Jul 02 '20 at 10:49
  • 1
    so for lazy loading we should add canDeactivate: [CanExitGuard] to lazy route path. `const routes: Routes = [ { path: "", component: MobilityStepComponent,canDeactivate: [CanExitGuard] }, ]; ` – Priyanka Jul 02 '20 at 10:51