backbone.js 打字稿错误:当类别包含初始化的属性时,'super'呼叫必须是建构函式中的第一个陈述式

31moq8wy  于 2022-11-10  发布在  其他
关注(0)|答案(2)|浏览(157)

我有以下打字错误在我的项目..让我分享一个样本,这样你就可以看到什么是处理。

module CoreWeb {
export class Controller implements IController {
    public $q;
    public $rootScope;
    public $scope:ng.IScope;
    public $state:ng.ui.IStateService;
    public $translate:ng.translate.ITranslateService;
    public appEvents;
    public commonValidationsService;
    public defaultPagingOptions = {
        currentPage: 1,
        pageSize: 10,
        totalServerItems: 0,
        maxSize: 5
    };
    public detailModelName:string;
    public filter:string;
    public listModelName:string;
    public mode;
    public modelDataService;
    public modelDefaultProperty:string;
    public ngDialog;
    public notificationsService;
    public pagingOptions:IPagingOptions;
    public selectionStatus:boolean;
    public serviceCreateFunction:string;
    public serviceGetAllCanceller:ng.IDeferred<any>;
    public serviceGetAllFunction:string;
    public serviceGetOneFunction:string;
    public serviceUpdateFunction:string;
    public showInactive:boolean;
    public tableAction:number;
    public tableActions:ITableAction[];
    public titleDataFactory;
    public validationOptions;
    public validationRules;
    public orderBy = null;
    public orderType = null;
    constructor(
        $q:ng.IQService,
        $rootScope,
        $scope:ng.IScope,
        $state,
        $translate:ng.translate.ITranslateService,
        appEvents,
        commonValidationsService,
        detailModelName:string,
        listModelName:string,
        modelDataService,
        modelDefaultProperty:string,
        ngDialog,
        notificationsService,
        serviceCreateFunction:string,
        serviceGetAllFunction:string,
        serviceGetOneFunction:string,
        serviceUpdateFunction:string,
        titleDataFactory
    ) {
        this.$q = $q;
        this.$rootScope = $rootScope;
        this.$scope = $scope;
        this.$state = $state;
        this.$translate = $translate;
        this.appEvents = appEvents;
        this.commonValidationsService = commonValidationsService;
        this.detailModelName = detailModelName;
        this.listModelName = listModelName;
        this.modelDataService = modelDataService;
        this.modelDefaultProperty = modelDefaultProperty;
        this.ngDialog = ngDialog;
        this.notificationsService = notificationsService;
        this.serviceCreateFunction = serviceCreateFunction;
        this.serviceGetAllCanceller = $q.defer();
        this.serviceGetAllFunction = serviceGetAllFunction;
        this.serviceGetOneFunction = serviceGetOneFunction;
        this.serviceUpdateFunction = serviceUpdateFunction;
        this.titleDataFactory = titleDataFactory;

        this.mode = $rootScope.modeEnum.none;
        this.pagingOptions = this.defaultPagingOptions;
        this.selectionStatus = false;
        this.showInactive = false;
        this.tableAction = null;
        this.tableActions = [
            {id: 1, name: "Activate"},
            {id: 2, name: "Deactivate"}
        ];
        this.validationOptions = {showErrors: commonValidationsService.modes.property, showNotification: true};

        this.activate();
    }

这是扩展控制器类的类...其中之一

declare var App: ng.IModule;

module CoreWeb {
    export class EntityMasterController extends Controller {
        private currenciesDataSet;
        private entity: IEntityMasterModel;
        private merchandisingConstants;
        private typeAheadOptions;

    constructor(
        $q:ng.IQService,
        $rootScope,
        $scope:ng.IScope,
        $state,
        $translate:ng.translate.ITranslateService,
        appEvents,
        commonValidationsService,
        entityDataService,
        merchandisingConstants,
        ngDialog,
        notificationsService,
        titleDataFactory
    ) {
        this.merchandisingConstants = merchandisingConstants;
        super(
            $q,
            $rootScope,
            $scope,
            $state,
            $translate,
            appEvents,
            commonValidationsService,
            "entity",
            null,
            entityDataService,
            "name",
            ngDialog,
            notificationsService,
            "createEntity",
            "getCurrentEntity",
            "getEntity",
            "updateEntity",
            titleDataFactory
        );
    }

现在,如果我像上面那样在super调用之前初始化merchandisingConstants,我会在gulp过程中得到以下错误,并且我的页面没有显示任何内容。当一个类包含初始化属性或参数属性时,super调用必须是构造函数中的第一条语句。我已经尝试了所有我能想到的方法来修复这些错误,你知道我该怎么做吗?

v440hwme

v440hwme1#

当您扩充类别时,您的建构函式:
1.必须调用super()
1.必须在执行其他操作之前执行此操作
在您的示例中,您只需要重新排序:

declare var App: ng.IModule;

module CoreWeb {
    export class EntityMasterController extends Controller {
        private currenciesDataSet;
        private entity: IEntityMasterModel;
        private merchandisingConstants;
        private typeAheadOptions;

    constructor(
        $q:ng.IQService,
        $rootScope,
        $scope:ng.IScope,
        $state,
        $translate:ng.translate.ITranslateService,
        appEvents,
        commonValidationsService,
        entityDataService,
        merchandisingConstants,
        ngDialog,
        notificationsService,
        titleDataFactory
    ) {
        // Must be first
        super(
            $q,
            $rootScope,
            $scope,
            $state,
            $translate,
            appEvents,
            commonValidationsService,
            "entity",
            null,
            entityDataService,
            "name",
            ngDialog,
            notificationsService,
            "createEntity",
            "getCurrentEntity",
            "getEntity",
            "updateEntity",
            titleDataFactory
        );

        this.merchandisingConstants = merchandisingConstants;
    }
b1uwtaje

b1uwtaje2#

这是一个相当复杂的问题,但它是一个简单的解决方法:

super(
        (this.merchandisingConstants = merchandisingConstants, $q),
        $rootScope,
        $scope,
        $state,
        $translate,
        appEvents,
        commonValidationsService,
        "entity",
        null,
        entityDataService,
        "name",
        ngDialog,
        notificationsService,
        "createEntity",
        "getCurrentEntity",
        "getEntity",
        "updateEntity",
        titleDataFactory
    );

它使用有点奇怪的JavaScript ,运算符来填充赋值。实际上,您可以对任何参数执行此操作,但我对第一个参数执行了此操作。逗号运算符与分隔函数参数的逗号 * 不同 *,即使是完全相同的字符--让你把一个表达式列表串在一起,所有的表达式都将被计算。只有最后一个表达式被用作整个表达式的值。
因此,通过这样做,你可以在参数列表求值的同时进行赋值。第一个参数的值仍然是$q,但赋值也会在函数调用super()之前进行。

相关问题