首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >canActivate和canDeactivate不一起工作

canActivate和canDeactivate不一起工作
EN

Stack Overflow用户
提问于 2019-01-23 17:13:56
回答 1查看 699关注 0票数 2

(角6)我的问题:

  • 路线A有canDeactivate,当我试图去B时,我需要确认。
  • 路由B有canActivate,而我没有访问权限。
  • 然后重定向到路由C,但在这一刻,canDeactivate从A再次请求确认。

如何跳过第二次确认?

代码语言:javascript
复制
export interface PendingChangesComponent {
canDeactivate: () => boolean | Observable<boolean>;
}

@Injectable()
export class PendingChangesGuard implements 
CanDeactivate<PendingChangesComponent>, OnInit, OnChanges {

constructor(private confirmationservice: ConfirmationService,
    private translate: TranslateService) {
    console.log('construtor d');
}

ngOnInit() {
    console.log('init d');
}

ngOnChanges() {
    console.log('on changes d');
}

canDeactivate(component: PendingChangesComponent): boolean | Observable<boolean> {
    debugger

    if (component.canDeactivate()) {
        return true;
    }
    return Observable.create((observer: Observer<boolean>) => {
        this.confirmationservice.confirm({
            message: this.translate.instant('can.deactivate'),
            accept: () => {
                observer.next(true);
                observer.complete();
            },
            reject: () => {
                observer.next(false);
                observer.complete();
            }
        });
    });
}
}
EN

回答 1

Stack Overflow用户

发布于 2019-02-14 09:54:32

当您需要导航到C时,应该在canDeactivate() component method中返回true .您可以设置一个特定的组件字段,并将其设置为true,如下所示:

代码语言:javascript
复制
// everything inside the A component


canDeactivate():boolean {
   return this.allowRedirect || yourOtherConditions;
}

// ... right before you need to navigate to C
this.allowRedirect = true;
this.router.navigate(["routeC"]);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54332393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档