在打字稿中调用 rest API 时面临错误"'this' implicitly has type 'any' because it does not have a type annotati



我正在使用TypeScript。我正试图在下面这样的ViewModel中调用rest API。

export class TestListViewModel {
public personItems: KnockoutObservableArray<Person>;
constructor() {
this.personItems = ko.observableArray([]);
$.get("https://somerestapi/api/TestLists", 
function (data: any) {
for (var index in data) {
this.personItems.push(data[index]);
} 
});
} //end of constructor
} //end of class

在this.personItems.push(data[index](行中,在"this"处,我收到编译错误,因为"this"隐式具有类型"any",因为它没有类型注释

在这里,我想访问回调函数中的Viewmodel变量。

请帮我怎么做。

我使用以下代码解决了这个问题。

$.get("https://somerestapi/api/TestLists", 
(data: any) => {
for (var index in data) {
this.personItems.push(data[index]);
}
});

相关内容

最新更新