Home » vue » "TypeError: __WEBPACK_IMPORTED_MODULE_1_jsplumb___default.a.draggable is not a function"

"TypeError: __WEBPACK_IMPORTED_MODULE_1_jsplumb___default.a.draggable is not a function"

Posted by: admin November 26, 2021 Leave a comment

Questions:

I’m trying to do drag and drop using JsPlumb in vueJS. I used JsPlumb link and also imported jsplumb but it is not working. I’m getting error as

[Vue warn]: Error in created hook: “TypeError:
__WEBPACK_IMPORTED_MODULE_1_jsplumb___default.a.draggable is not a function”

This code is written inside the template

<div class="center_block">
  <ul id="menu">
    <li><img class="decision" src="../assets/images/symbol_decision.png" alt="human_gif"></li>
    <li><img class="input" src="../assets/images/symbol_input1.png" alt="human_gif"></li>
  </ul>
</div>
<div class="layout" id="layoutId" style="height:404px;"></div>

This code is written inside script

import jsPlumb from 'jsplumb'
export default {
  name: 'HelloWorld',
  data() {
    return {
      message: 'Welcome user',
      endpoint1: '',
      i: 0,
      clone: '',
      componentMap: {}
    }
  },
  created() {
    this.endpoint1 = {
      isSource: true,
      isTarget: true,
      endpoint: ['Dot', {
        radius: 4
      }, {
        outlineStroke: 'black'
      }, {
        outlineWidth: 10
      }],
      paintStyle: {
        outlineStroke: 'black',
        outlineWidth: 10
      },
      cssClass: 'ends',
      maxConnections: -1,
      connector: ['Flowchart', {
        stub: [40, 60],
        gap: 5,
        cornerRadius: 5,
        alwaysRespectStubs: true
      }],
      connectorStyle: {
        strokeWidth: 2,
        stroke: '#61B7CF'
      },
      connectorHoverClass: 'connector-line',
      dropOptions: {
        drop: function(e, ui) {
          alert('drop!')
        }
      }
    }
    jsPlumb.draggable('decision', {
      appendTo: 'body',
      cursor: 'pointer',
      containment: 'layoutId',
      helper: 'clone',
      revert: 'invalid'
    })

    window.jsPlumbInstance.droppable('layout', {
      accept: '.decision, .input',
      activeClass: 'ui-state-default',
      hoverClass: 'ui-state-hover',
      drop: function(event, ui) {
        this.clone = ui.helper.clone()
        this.clone.draggable({
          containment: 'layout'
        })

        if (ui.draggable.attr('id') !== null) {
          if (ui.draggable.attr('class').indexOf('decision') !== -1) {
            this.clone.attr('id', 'input_' + this.i)
            this.componentMap['input_' + this.i] = this.clone.attr('name')
            jsPlumb.addEndpoint(this.clone, {
              anchors: ['Left']
            }, this.endpoint1)
            jsPlumb.addEndpoint(this.clone, {
              anchors: ['Right']
            }, this.endpoint1)
          } 
        }
      }
    })
  }
}

can anyone please suggest me where I did the mistake?

Answers:

I had the same issue and fixed it by importing jsPlumb this way:

import jsplum from 'jsplumb'

And then the calls should occurs like this:

jsplumb.jsPlumb.ready(function () {
  jsplumb.jsPlumb.connect({
    source: 'item0',
    target: 'item1',
    endpoint: 'Rectangle'
  })

###

jsPlumb seems to be a non default export.

Do this:

import {jsPlumb} from 'jsplumb';