在mithril-js中,vnode如何绑定到click事件

时间:2018-03-25 09:21:11

标签: javascript mithril.js

在Mithril.js中(使用webpack),我需要将vnode传递给匿名函数:

怎么做?

var Button = {
  view:function(vnode){
    return m('div',{onclick:(vnode)=> setSort(vnode)})
  }
}

function setSort(vnode){
  .... do somthing with vnode ....
}

1 个答案:

答案 0 :(得分:2)

Event handlers get passed the event object, not a vnode. You already have access to the vnode via closures, it's passed to your view method as the first argument.

const Button = {
    view(vnode) {
        return m("button", {
            onclick() {
                console.log(vnode);
            }
        }, "Button");
    }
};

m.mount(document.body, {
    view() {
        return m("div",
            m(Button)
        );
    }
});

Here's a running example on flems.io.