Vuejs方法没有开火

krr*_*r25 1 jquery d3.js vue.js dagre-d3 vuejs2

当我从jquery选择器调用vuejs函数时,它不会触发.我在vue组件中集成了d3-dagre图表.当我为节点设置单击操作时,它不会触发vuejs方法.在下面getJobid()没有触发.找到下面的vue组件代码,最后也是错误.

Vue组件:

export default {
  name: 'dagView',
  created () {
    console.log('test2')
  },
  mounted () {
     var g = new dagreD3.graphlib.Graph().setGraph({})
     // Create the renderer    
     var render = new dagreD3.render()

     // Set up an SVG group so that we can translate the final graph.
     var svg = d3.select("svg"),
     inner = svg.append("g")

     // Run the renderer. This is what draws the final graph.
     render(inner, g)

     inner.selectAll("g.node")
     .on("click", function(v) {
        console.log("Nodes --> "+ v + " -- "+ g.node(v).label)
        this.nodeId = g.node(v).label
        console.log("Node id -- "+ this.nodeId)
        this.getJobid()
     })
  },
  methods: {
    getJobid: function() {
      console.log('Received NodeId')
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

错误:

Uncaught TypeError: this.getJobid is not a function
at SVGGElement.eval (DAG.vue?2ccc:180)
at SVGGElement.__onclick (d3.v3.min.js:1)
Run Code Online (Sandbox Code Playgroud)

tha*_*ksd 5

this回调的on处理程序未引用Vue的实例.在处理程序外部设置引用并使用它:

var self = this 
inner.selectAll("g.node")
  .on("click", function(v) {
    console.log("Nodes --> "+ v + " -- "+ g.node(v).label)
    self.nodeId = g.node(v).label
    console.log("Node id -- "+ this.nodeId)
    self.getJobid()
  })
Run Code Online (Sandbox Code Playgroud)