简体   繁体   English

确认用户在编辑Angular 2时无需保存详细信息即可更改路线

[英]Confirm User to Change Route without saving details while editing Angular 2

If the user is editing something and try to change the route without saving details. 如果用户正在编辑内容,并尝试更改路线而不保存详细信息。

in this condition I want to alter the user that 'your changes are not saved do you want to go away'. 在这种情况下,我想更改用户“您的更改没有保存,您想消失吗”。 according to the user can we prevent component to destroy the component. 根据用户可以防止组件破坏组件。

I try a guard but guard run before ngOnDestroy method run. 我尝试了一个防护,但是在ngOnDestroy方法运行之前先进行防护。 So what should I do? 所以我该怎么做?

我已经使用CanDeactivate来实现相同的功能,并且效果很好。

trigger NavigationStart of Router to do jobs before leaving current route. 在离开当前路线之前,触发Router的NavigationStart做作业。

constructor(private router: Router) {
  ..
  this.router.events.forEach((e: any) => {
    if (e instanceof NavigationStart) {
      // do confirm here.
    }
  });
}

You can use angular CanDeactivate for this purpose . 您可以为此使用角度CanDeactivate。

Link of canDeactivate Angular canDeactivate Angular的链接

https://angular.io/docs/js/latest/api/router/index/CanDeactivate-interface.html https://blog.thoughtram.io/angular/2016/07/18/guards-in-angular-2.html https://angular.io/docs/js/latest/api/router/index/CanDeactivate-interface.html https://blog.thoughtram.io/angular/2016/07/18/guards-in-angular-2 html的

Also a working example @ https://rahulrsingh09.github.io/AngularConcepts/ 也是一个工作示例@ https://rahulrsingh09.github.io/AngularConcepts/

AdvancedConcepts - > guards AdvancedConcepts->守卫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM