Typescript错误:当一个类包含初始化属性时,'super'调用必须是构造函数中的第一个语句
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Typescript错误:当一个类包含初始化属性时,'super'调用必须是构造函数中的第一个语句相关的知识,希望对你有一定的参考价值。
我的项目中有以下打字错误。让我分享一个示例,以便您可以看到正在处理的内容。
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
);
}
现在,如果我在上面的超级调用之前初始化merchandisingConstants
..我在gulp期间得到以下错误,我的页面没有显示任何内容..当一个类包含初始化属性时,super
调用必须是构造函数中的第一个语句有参数属性。我已经尝试了各种方法,我可以考虑解决这个错误的任何想法,我怎么能这样做?
当你扩展一个类时,你的构造函数:
- 必须打电话给
super()
- 必须在它做任何事之前做到这一点
在您的实例中,您只需要重新排序:
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;
}
这是一个非常黑客,但它是一个简单的解决方法:
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()
进行函数调用之前,也会发生赋值。
以上是关于Typescript错误:当一个类包含初始化属性时,'super'调用必须是构造函数中的第一个语句的主要内容,如果未能解决你的问题,请参考以下文章
错误 TypeError:无法读取未定义的属性(读取“跳过”)Typescript/Api
使用 Typescript 时如何停止“类型 JQuery 上不存在属性”语法错误?
如何从Typescript中以#或@开头的属性获取JSON数据