TypeScript:undefined 不是一个函数
TypeScript: undefined is not a function
我有以下 ts-class:
class TrackerManager {
public _csTrackers: DevExpress.data.CustomStore;
public _dbOptions: DevExpress.data.CustomStoreOptions;
private SERVICE_URL_GET_TRACKERS: string = 'xx'
private SERVICE_URL_UPDATE_TRACKER: string = 'xx'
private SERVICE_URL_GET_GPSSYSTEMS: string = 'xx'
constructor() {
this.initTrackersCustomStoreSettings();
}
private initTrackersCustomStoreSettings(): void {
this._dbOptions = {
load: function (loadOptions):any {
debugger;
return this.SendRequest(this.SERVICE_URL_GET_TRACKERS, 'GET');
},
insert: function (params):any {
//return dbImpl._sendRequest(SERVICE_URL_UPDATE_TRACKER, 'POST', params);
},
};
}
public SendRequest(myurl: string, t: string): any {
//ToDo
}
public getTrackerCustomStore(): DevExpress.data.CustomStore {
this._csTrackers = new DevExpress.data.CustomStore(this._dbOptions);
return this._csTrackers;
}
}
调用 load 时,谁能告诉我为什么 this.SendRequest 未定义?我必须改变什么?
我是打字稿的新手。
非常感谢。
最好的问候
问题
现在您的代码的 JavaScript 输出如下:
TrackerManager.prototype.initTrackersCustomStoreSettings = function () {
this._dbOptions = {
load: function (loadOptions) {
debugger;
return this.SendRequest(this.SERVICE_URL_GET_TRACKERS, 'GET');
},
// omitted...
};
};
如您所见,this
的值将不会保留。
解决方案
要解决此问题,请使用箭头函数来保留上下文:
private initTrackersCustomStoreSettings(): void {
this._dbOptions = {
load: (loadOptions) => {
debugger;
return this.SendRequest(this.SERVICE_URL_GET_TRACKERS, 'GET');
},
// omitted...
};
}
这将转译为以下 javascript:
TrackerManager.prototype.initTrackersCustomStoreSettings = function () {
var _this = this; // notice this new variable and how it's used
this._dbOptions = {
load: function (loadOptions) {
debugger;
return _this.SendRequest(_this.SERVICE_URL_GET_TRACKERS, 'GET');
},
// omitted...
};
};
由于您还没有实现 SendRequest
方法,因此它没有 return 值。因此,当您尝试 return SendRequest
的结果时,您会得到未定义的结果。
我有以下 ts-class:
class TrackerManager {
public _csTrackers: DevExpress.data.CustomStore;
public _dbOptions: DevExpress.data.CustomStoreOptions;
private SERVICE_URL_GET_TRACKERS: string = 'xx'
private SERVICE_URL_UPDATE_TRACKER: string = 'xx'
private SERVICE_URL_GET_GPSSYSTEMS: string = 'xx'
constructor() {
this.initTrackersCustomStoreSettings();
}
private initTrackersCustomStoreSettings(): void {
this._dbOptions = {
load: function (loadOptions):any {
debugger;
return this.SendRequest(this.SERVICE_URL_GET_TRACKERS, 'GET');
},
insert: function (params):any {
//return dbImpl._sendRequest(SERVICE_URL_UPDATE_TRACKER, 'POST', params);
},
};
}
public SendRequest(myurl: string, t: string): any {
//ToDo
}
public getTrackerCustomStore(): DevExpress.data.CustomStore {
this._csTrackers = new DevExpress.data.CustomStore(this._dbOptions);
return this._csTrackers;
}
}
调用 load 时,谁能告诉我为什么 this.SendRequest 未定义?我必须改变什么?
我是打字稿的新手。
非常感谢。 最好的问候
问题
现在您的代码的 JavaScript 输出如下:
TrackerManager.prototype.initTrackersCustomStoreSettings = function () {
this._dbOptions = {
load: function (loadOptions) {
debugger;
return this.SendRequest(this.SERVICE_URL_GET_TRACKERS, 'GET');
},
// omitted...
};
};
如您所见,this
的值将不会保留。
解决方案
要解决此问题,请使用箭头函数来保留上下文:
private initTrackersCustomStoreSettings(): void {
this._dbOptions = {
load: (loadOptions) => {
debugger;
return this.SendRequest(this.SERVICE_URL_GET_TRACKERS, 'GET');
},
// omitted...
};
}
这将转译为以下 javascript:
TrackerManager.prototype.initTrackersCustomStoreSettings = function () {
var _this = this; // notice this new variable and how it's used
this._dbOptions = {
load: function (loadOptions) {
debugger;
return _this.SendRequest(_this.SERVICE_URL_GET_TRACKERS, 'GET');
},
// omitted...
};
};
由于您还没有实现 SendRequest
方法,因此它没有 return 值。因此,当您尝试 return SendRequest
的结果时,您会得到未定义的结果。