我试图在路由器上覆盖 deactive
方法,具体如下:
export class AnimationRouterOutlet扩展RouterOutlet {publicRoutes:any; private parentRouter:路由器;
constructor(_elementRef: ElementRef, _loader: DynamicComponentLoader,
_parentRouter: Router, @Attribute('name') nameAttr: string) {
super(_elementRef, _loader, _parentRouter, nameAttr);
this.parentRouter = _parentRouter;
}
deactivate(instruction: ComponentInstruction){
setTimeout(()=>{
console.log("deactivvated state");
return super.deactivate(instruction);
},1000);
}
activate(instruction: ComponentInstruction) {
console.log(instruction);
return super.activate(instruction);
}
}
正如您所看到的,我正在尝试在更改网页时创建延迟 - 以便我可以创建一些动画 .
但是,使用这个,我有TypeScript编译器错误:
:错误TS2415:类'AnimationRouterOutlet'错误地扩展了基类'RouterOutlet' . 属性“停用”的类型不兼容 . 类型'(instruction:ComponentInstruction)=> void'不能分配给'(nextInstruction:ComponentInstruction)=> Promise' . 类型'void'不能分配给'Promise'类型 .
我怎样才能解决这个问题?
1 回答
您不能以这种方式更改被覆盖函数的返回类型 . 返回类型需要与超类中的返回类型相同或更专业的类型 .
如果实现CanDeactivate,则可以在路由器离开组件之前等待路由器等待的承诺 .