如何重写去激活方法在路由器? [英] How to override deactive method in Router?

查看:193
本文介绍了如何重写去激活方法在路由器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重写去激活方法对路由器与以下内容:

I am trying to override deactive method on Router with the following:

出口类AnimationRouterOutlet扩展RouterOutlet {
    publicRoutes:任;
    私人parentRouter:路由器;

export class AnimationRouterOutlet extends RouterOutlet { publicRoutes: any; private parentRouter: Router;

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);
}

}

正如你所看到的,我想改变网页创建延迟 - 这样我可以创造一些动画

As you can see, I am trying to create delay in changing webpage - so that I can create some animations.

不过,用这个,我有打字稿编译器错误:

However, using this, I have TypeScript compiler error:

:错误TS2415:类'AnimationRouterOutlet不正确基地延伸
  类的RouterOutlet。物业类型停用是
  不相容。
      类型'(指令:ComponentInstruction)=>无效'是不能分配给输入(nextInstruction:ComponentInstruction)=>
  诺言'。
        类型'无效'是不能分配给键入'无极'。

: error TS2415: Class 'AnimationRouterOutlet' incorrectly extends base class 'RouterOutlet'. Types of property 'deactivate' are incompatible. Type '(instruction: ComponentInstruction) => void' is not assignable to type '(nextInstruction: ComponentInstruction) => Promise'. Type 'void' is not assignable to type 'Promise'.

我该如何解决这个问题?

How can I fix this?

推荐答案

您不能更改重写功能,这样的返回类型。返回类型需要是相同类型的超类的返回类型或更专门的之一。

You can't change the return type of an overridden function that way. The return type needs to be the same type as the return type in the super class or a more specialized one.

如果您实现 CanDeactivate 你可以承诺当路由器等待之前的路线从组件了。

If you implement CanDeactivate you can a promise where the router waits for before it routes away from the component.

这篇关于如何重写去激活方法在路由器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆