在 TypeScript 中,创建具有 http 构造函数的相同 class 的对象
In TypeScript, create object of same class having constructor of http
export class FloorPlanComponent implements AfterViewInit, OnInit {
constructor();
constructor(public _baseComponentService?: BaseComponentService,
public _internalZoneService?: InternalZoneService,
public routeParams?: ActivatedRoute,
public internalAssetService?: InternalAssetService,
public slimLoadingBarService?: SlimLoadingBarService,
private errorLoggerService?: ErrorLoggerService
) {
if (this._baseComponentService != null) {
this._baseComponentService.SetPageTitle("Zones - Internal");
}
this.imgDelZone = parentElement.append("image")
.attr("id", "delZone" + newId)
.attr("idCounter", newId)
.attr("r", "NaN")
.attr("stroke-width", 0)
.attr("opacity", 0.8)
.attr("x", function (d) { return d.x + (shapeWidth - 22); })
.attr("y", function (d) { return d.y + 1; })
.attr("style", "opacity: 1; stroke-width: 1;")
.attr("xlink:href", "../app/assets/images/Remove_Delete-22.png")
.attr("preserveAspectRatio", "none")
.attr("transform", " ")
.attr("height", 18)
.attr("width", 18)
.attr("cursor", "pointer").on("click", function () {
var comp = new FloorPlanComponent();
comp.promptDeleteZone(this);
});
}
在 "click" 函数中,我想用它来引用我的 class。但它将其称为 window 对象。为了解决这个问题,我在无参数构造函数的帮助下创建了一个 class 的对象。但是据此,我无法获得任何服务。请帮忙
你可能会用到箭头函数:
attr("cursor", "pointer").on("click", () => {
...
});
箭头函数捕获创建函数的 this
,因此 this
将指向 FloorPlanComponent 实例。
怎么样
.attr("cursor", "pointer").on("click", this.onClicked.bind(this));
.............
onClicked() {
var comp = new FloorPlanComponent();
comp.promptDeleteZone(this);
}
export class FloorPlanComponent implements AfterViewInit, OnInit {
constructor();
constructor(public _baseComponentService?: BaseComponentService,
public _internalZoneService?: InternalZoneService,
public routeParams?: ActivatedRoute,
public internalAssetService?: InternalAssetService,
public slimLoadingBarService?: SlimLoadingBarService,
private errorLoggerService?: ErrorLoggerService
) {
if (this._baseComponentService != null) {
this._baseComponentService.SetPageTitle("Zones - Internal");
}
this.imgDelZone = parentElement.append("image")
.attr("id", "delZone" + newId)
.attr("idCounter", newId)
.attr("r", "NaN")
.attr("stroke-width", 0)
.attr("opacity", 0.8)
.attr("x", function (d) { return d.x + (shapeWidth - 22); })
.attr("y", function (d) { return d.y + 1; })
.attr("style", "opacity: 1; stroke-width: 1;")
.attr("xlink:href", "../app/assets/images/Remove_Delete-22.png")
.attr("preserveAspectRatio", "none")
.attr("transform", " ")
.attr("height", 18)
.attr("width", 18)
.attr("cursor", "pointer").on("click", function () {
var comp = new FloorPlanComponent();
comp.promptDeleteZone(this);
});
}
在 "click" 函数中,我想用它来引用我的 class。但它将其称为 window 对象。为了解决这个问题,我在无参数构造函数的帮助下创建了一个 class 的对象。但是据此,我无法获得任何服务。请帮忙
你可能会用到箭头函数:
attr("cursor", "pointer").on("click", () => {
...
});
箭头函数捕获创建函数的 this
,因此 this
将指向 FloorPlanComponent 实例。
怎么样
.attr("cursor", "pointer").on("click", this.onClicked.bind(this));
.............
onClicked() {
var comp = new FloorPlanComponent();
comp.promptDeleteZone(this);
}