gpt4 book ai didi

javascript - 从 ngOnInit 向函数传递值

转载 作者:行者123 更新时间:2023-11-28 18:32:47 26 4
gpt4 key购买 nike

我有一个调用剪辑功能的按钮。我需要将值从 ngOnInit 传递到该函数,但我不能。我收到(例如)this.firstX 的未定义错误。如何将值从 ngOnInit 传递到函数?

export class AppAppComponent implements OnInit {

public firstX: number;
public firstY: number;
public lastX: number;
public lastY: number;

constructor(mdIconRegistry: MdIconRegistry) {
mdIconRegistry
.registerFontClassAlias('fontawesome', 'fa');
}

ngOnInit() {

let canvas = new fabric.Canvas('cLeft');
let src = "../../images/kupurSecond.jpg";

canvas.on('mouse:down', function (event) {
var position = canvas.getPointer(event.e);
this.firstX = position.x;
this.firstY = position.y;
});

canvas.on('mouse:up', function (event) {
var position = canvas.getPointer(event.e);
this.lastX = position.x;
this.lastY = position.y;
// ReLoading Image
loadImage(src, canvas, this.firstX, this.firstY, this.lastX, this.lastY);
});

loadImage(src, canvas);

};

public clip() {
console.log(this.firstX);
}

}

最佳答案

您必须使用箭头符号来保持相同的this上下文。

canvas.on('mouse:down', (event) => {
var position = canvas.getPointer(event.e);
this.firstX = position.x;
this.firstY = position.y;
});

mouse:up 函数也是如此。只要永远不要在 typescript 中使用“函数”这个词,你就很优秀

关于javascript - 从 ngOnInit 向函数传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37660226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com