web-dave / Angular-Essentials-Training

9 stars 0 forks source link

Guards #19

Open web-dave opened 6 years ago

web-dave commented 6 years ago
web-dave commented 6 years ago

generate (DEPRECATED!!!)

  ng g guard books/shared/leave
web-dave commented 6 years ago

leave.guard.ts


import { BookNewComponent } from './../book-new/book-new.component';

import { CanDeactivateFn } from '@angular/router';

@Injectable()
export const leaveGuard: CanDeactivateFn<BookNewComponent> = (target: BookNewComponent) => {
   if (!target.isSaved()) {
     return window.confirm('Do you really want to cancel?');
   } else {
     return true;
   }
 }
web-dave commented 6 years ago

books.module.ts (DEPRECATED!!!)


 import ...

  @NgModule({
  imports: [...],
  exports: [],
  declarations: [...],
  providers: [LeaveGuard]
  })
  export class BooksModule { }
web-dave commented 6 years ago

books-routing.module.ts


    {
      path: 'new',
      component: BookNewComponent,
      canDeactivate: [leaveGuard]
    }
web-dave commented 6 years ago

book-new.component.ts



  isSaved() {
    return this.saved || !this.form.dirty;
  }
web-dave commented 6 years ago

PreloadingStrategy