【发布时间】:2015-10-20 11:34:24
【问题描述】:
我的项目中有以下打字稿错误..让我分享一下 一个示例,以便您了解正在处理的内容。
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参数列表中。 -
将
this.merchandisingConstants赋值移动到super调用下方有什么问题? -
我认为从错误消息中可以很明显地看出,您只需将
super(..作为构造函数中的第一条语句移动即可。 -
@pointy 我该怎么做?
-
使
super()的第一个参数为(this.merchandisingConstants = merchandisingConstants, $q)- 这将具有在super()调用发生之前进行赋值的效果,它还将传递$q的值只是就像在你的代码中一样。确保包含括号,以便,被解释为逗号运算符,而不是参数列表中的逗号分隔符。
标签: javascript angularjs backbone.js typescript