javascript - VueJS Custom Directive Function as Argument -
i've got simple directive:
html:
<div id="app"> <div v-sample:callback="greet"></div> </div> <script> var app = new vue({ el: "#app", data: { files: [], }, methods: { greet: function (arg) { alert(arg); }, }, }); </script> js:
vue.directive('sample', { bind: function (el, binding, vnode) { el.addeventlistener('...', function () { // ... callback.call(arg, ...); }); }, }); however, i'm unclear of appropriate syntax function , evaluate. what's proper way within directive?
you can use binding.value should function in case. it's prebound correct context call (pass in if need something):
vue.directive('sample', { bind: function (el, binding, vnode) { el.addeventlistener('click', function () { binding.value() }); }, });
Comments
Post a Comment