【发布时间】:2021-10-16 03:24:38
【问题描述】:
我在以下结构的辅助类中丢失了“This”对象的上下文,我不知道为什么。
在 getAll 方法中,this 对象正在引用位于主组件的 servicesDict 数组中的对象。 p>
我希望 this 对象引用 Entity1Utils 类。
export class Entity1Utils {
public getAll(context) {
this.buildQueryParams();
// this "this" refers to the object { id: 'entity1', method: this.entity1Utils.getAll }
// located on servicesDict at ManagementComponent
}
private buildQueryParams() {
// logical code
}
}
@Component({
selector: 'app-management',
templateUrl: './management.component.html',
styleUrls: ['./management.component.css']
})
export class ManagementComponent implements OnInit {
private entity1Utils: Entity1Utils;
private servicesDict: any;
constructor() {
this.entity1Utils = new Entity1Utils();
this.servicesDict = [
{ id: 'entity1', method: this.entity1Utils.getAll }
];
}
}
【问题讨论】:
-
对不起,我不太明白你在说哪个
this,它指向哪里以及你希望它指向哪里。
标签: javascript angularjs typescript