2

I'm trying to convert JSX code from example at http://element.eleme.io/#/en-US/component/tree#custom-node-content to Vue $createElement code

original code snippet:

renderContent(h, { node, data, store }) {
    return (
      <span>
        <span>
          <span>{node.label}</span>
        </span>
        <span style="float: right; margin-right: 20px">
          <el-button size="mini" on-click={ () => this.append(store, data) }>Append</el-button>
          <el-button size="mini" on-click={ () => this.remove(store, data) }>Delete</el-button>
        </span>
      </span>);
    }
}

and my attempt to transform:

  renderContent (h, { node, data, store }) {
    return h('SPAN', [
      h('SPAN', [h('SPAN', node.label)]),
      h('SPAN', {attrs: {style: 'float: right; margin-right: 20px'}}, [
        h('el-button', { attrs: { size: 'mini', on: { click: this.append(store, data) } } }, 'Append'),
        h('el-button', { attrs: { size: 'mini', on: { click: this.delete } } }, 'Delete')
      ])
    ])
  }

but it fails: the button Append always executed at the time of rendering for every node and never after. While button Delete is never executed at all.

So the question is how to pass an event handler with arguments properly to the created element?

0

1 Answer 1

3

It's because you're actually calling the append function when rendering the content. Try this:

{ 
    attrs: { size: 'mini' }, 
    on: { click: this.append.bind(this, store, data) } 
}

or use the same solution as in the JSX snippet:

{ 
    attrs: { size: 'mini' },
    on: { click: () => this.append(store, data) } 
}

Both ways provide a function to be called when the click event occurs.

Notice that the on key is sibling to attrs, not inside.

Sign up to request clarification or add additional context in comments.

4 Comments

first approach gives the error : Error in render function: "TypeError: Cannot read property 'bind' of undefined", the second - no errors, but does nothing
That's strange... How it's then possible that this.append(store, data) in your code doesn't throw the error?
you are right I mistyped function name. So no errors and still event handler is not being triggered. Then I checked documentation again and found out that put on key to a wrong place - inside attrs definition. After fixed - all works fine. Thank you so much!
@htonus right, I haven't notice the wrong placement of the on key. It's good that you figured it out. I'm updating my answer to reflect this aspect too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.