我使用带有角的interactJS使用户能够使用类'draggable‘执行元素的拖放。一切都很好,直到我需要在末拖的回调函数中使用组件的注入服务。这就是我试过的:
ngOnInit() {
interact('.draggable')
.draggable({
inertia: true,
modifiers: [
interact.modifiers.restrictRect({
restriction: 'parent',
endOnly: true
})
],
autoScroll: true,
listeners: { move: this.dragMoveListener, end: this.stackForUndo }
})
}
stackForUndo() {
// get current state of svg
let current_state = document.getElementById("Logo").outerHTML;
// clear redo stack
this.menuService.redo = {}
// get the previous saved actions
let actions = { elements: [] };
// if no action has been cached, initialise the localStorage undo element
if (actions == undefined) {
actions = { elements: [] };
// save stringified empty action to localStorage
this.menuService.undo = actions;
}
// add the current state of the svg
if(actions.elements.length >= 5){
actions.elements.shift();
}
actions.elements.push(current_state);
this.menuService.undo = actions;
}拖动后,我无法在浏览器控制台中删除元素并获得折叠错误
无法读取未定义( this.menuService.redo = {})
的属性menuService
发布于 2020-11-29 01:34:45
问题的根源是stackForUndo丢失了上下文,为了解决这个问题,我将其绑定到组件,如下所示:
ngOnInit() {
interact('.draggable')
.draggable({
inertia: true,
modifiers: [
interact.modifiers.restrictRect({
restriction: 'parent',
endOnly: true
})
],
autoScroll: true,
listeners: { move: this.dragMoveListener, end: this.stackForUndo.bind(this) }
})
}https://stackoverflow.com/questions/65025790
复制相似问题