我有这段代码:
export class Profile {
private resource: Resource = new Resource();
/**
* Problem here
*/
async initialize(): Promise<void> {
console.log(this.resource);
var html = await this.resource.fetch(true);
const $profile = jQuery(html);
console.log($profile.find("span.largetext"));
}
}
如果你能看到这行代码
console.log(this.resource)
,我得到了undefined。异步方法无法访问“this”吗?我还使用了
console.log(this)
来测试,它在web检查器中返回Profile { }
。有没有办法可以访问
this
?
this
的值。但我刚才意识到你说它会引用一个Profile
实例。在这种情况下,由于某种原因,this
没有resource
属性。 - Felix Kling