Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular canActivate和canDeactivate don';我们不能一起工作_Angular_Angular6 - Fatal编程技术网

Angular canActivate和canDeactivate don';我们不能一起工作

Angular canActivate和canDeactivate don';我们不能一起工作,angular,angular6,Angular,Angular6,(6)我的问题: 路线A具有canDeactivate,当我尝试前往路线B时,我需要确认 路线B已激活,我无权访问 然后重定向到路径C,但此时,路径A的canDeactivate再次请求确认 如何跳过第二次确认 export interface PendingChangesComponent { canDeactivate: () => boolean | Observable<boolean>; } @Injectable() export class PendingC

(6)我的问题:

  • 路线A具有canDeactivate,当我尝试前往路线B时,我需要确认
  • 路线B已激活,我无权访问
  • 然后重定向到路径C,但此时,路径A的canDeactivate再次请求确认
如何跳过第二次确认

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();
            }
        });
    });
}
}
导出接口挂起更改组件{
canDeactivate:()=>布尔|可观察;
}
@可注射()
导出类PendingChangesGuard实现
CanDeactivate、OnInit、OnChanges{
构造函数(私有确认服务:确认服务,
私人翻译(翻译服务){
console.log('construtor d');
}
恩戈尼尼特(){
console.log('initd');
}
ngOnChanges(){
console.log('on changes d');
}
canDeactivate(组件:PendingChangesComponent):布尔值|可观察{
调试器
if(component.canDeactivate()){
返回true;
}
返回可观察的。创建((观察者:观察者)=>{
这是。确认服务。确认({
消息:this.translate.instant('can.deactivate'),
接受:()=>{
观察者:下一个(正确);
observer.complete();
},
拒绝:()=>{
观察者:下一个(假);
observer.complete();
}
});
});
}
}

当您需要导航到C。。。您可以设置特定的组件字段,并将其设置为true,如下所示:

// 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"]);