从 ngOnInit 向函数传递值
Passing Value to Functions From ngOnInit
我有一个调用 clip 函数的按钮。我需要将值从 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
函数也是如此。永远不要在打字稿中使用 function
这个词,你就是黄金
我有一个调用 clip 函数的按钮。我需要将值从 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
函数也是如此。永远不要在打字稿中使用 function
这个词,你就是黄金