首页 > 解决方案 > ReferenceError: 在使用 |this| 之前必须调用超级构造函数 在 Ti 类构造函数中

问题描述

我正在开发一个 Angular 项目,最近从 Angular 5.2.10 更新到 Angular 6。在升级之前一切正常。

现在,每次我为生产编译我的代码时,虽然在编译期间没有任何错误,但我在运行时收到此错误:

ReferenceError: must call super constructor before using |this| in Ti class constructor

唯一使用 super 的打字稿类是

export class Exception extends Error {
    private _statusCode: number;

    constructor(statusCode: number, type: ExceptionType) {
        super(ExceptionType[type]);
        this._statusCode = statusCode;
    }

    get statusCode(): number {
        return this._statusCode;
    }
}

简单明了。ExceptionType 是一个枚举。

该错误很容易解释,但似乎我做对了,因为 Error 类构造函数有一个单一的可选参数

Error(errorMsg?:string)

这是在“超级”事物之后调用的。

我麻木了。

编辑:尝试了评论中的建议,对我不起作用

编辑 2:正如@estus 指出的那样,错误不在此代码中,而是在

class Ei {
            constructor(l, n=Ei.now) {
                this.SchedulerAction = l,
                this.now = n
            }
            schedule(l, n=0, e) {
                return new this.SchedulerAction(this,l).schedule(e, n)
            }
        }
        Ei.now = Date.now ? Date.now : ()=>+new Date;
        class Ti extends Ei {
            constructor(l, n=Ei.now) {
                this.actions = [],
                this.active = !1,
                this.scheduled = void 0
            }
    ...
}

受影响的行是

this.actions = []

这与我的代码无关。我将在 github 上打开一个问题。

编辑 3:请检查https://github.com/angular/angular-cli/issues/7799#issuecomment-386582136

标签: angulartypescript

解决方案


推荐阅读