【发布时间】:2018-08-27 17:30:43
【问题描述】:
我正在尝试在我的组件中添加CanDeactivate 功能。我有一个
包含one input 字段和按钮的表单。我想如果用户在输入字段中输入内容并移动到下一个 screen 而不提交它将显示一个对话框。如果用户从对话框 bix 输入 yes 然后它转到下一个组件,否则它会保持在同一个屏幕中。
这是我的代码 https://stackblitz.com/edit/angular-ctwnid?file=src%2Fapp%2Fhello.component.ts
import {CanDeactivate} from '@angular/router';
import { HelloComponent } from './hello.component';
export default class DeactivateGuard implements CanDeactivate<HelloComponent> {
canDeactivate(component: HelloComponent): boolean {
if (!component.canDeactivate()) {
if (confirm('You have unsaved changes! If you leave, your changes will be lost.')) {
return true;
} else {
return false;
}
}
return true;
}
}
目前,当我在 input 字段上输入内容并单击 next 按钮时,它给了我错误
ERROR
Error: Uncaught (in promise): TypeError: Cannot read property 'ngInjectableDef' of undefined
TypeError: Cannot read property 'ngInjectableDef' of undefined
at resolveNgModuleDep (https://angular-ctwnid.stackblitz.io/turbo_modules/@angular/core@6.0.0/bundles/core.umd.js:9309:31)
at NgModuleRef_.get (https://angular-ctwnid.stackblitz.io/turbo_modules/@angular/core@6.0.0/bundles/core.umd.js:10003:16)
at PreActivation.getToken (https://angular-ctwnid.stackblitz.io/turbo_modules/@angular/router@6.0.0/bundles/router.umd.js:3014:25)
at MergeMapSubscriber.eval [as project] (https://angular-ctwnid.stackblit
【问题讨论】:
标签: javascript angular