Open web-dave opened 6 years ago
ng g guard books/shared/leave
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;
}
}
import ...
@NgModule({
imports: [...],
exports: [],
declarations: [...],
providers: [LeaveGuard]
})
export class BooksModule { }
{
path: 'new',
component: BookNewComponent,
canDeactivate: [leaveGuard]
}
isSaved() {
return this.saved || !this.form.dirty;
}
leave
guardnew
route