首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用interactJS在端回调函数中使用可注入服务

使用interactJS在端回调函数中使用可注入服务
EN

Stack Overflow用户
提问于 2020-11-26 16:23:56
回答 1查看 156关注 0票数 1

我使用带有角的interactJS使用户能够使用类'draggable‘执行元素的拖放。一切都很好,直到我需要在末拖的回调函数中使用组件的注入服务。这就是我试过的:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-29 01:34:45

问题的根源是stackForUndo丢失了上下文,为了解决这个问题,我将其绑定到组件,如下所示:

代码语言:javascript
复制
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) }
        })
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65025790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档