admin管理员组文章数量:1429053
I have directive and I wanted to create a local method inside the directive and use it in the hook function. Here's my code:
export const OutsideClick = {
bind (el, binding, vnode) {
console.log(new Vue());
// call method1()
},
ponentUpdated(el, binding, vnode) {
console.log('updated p', binding);
if(binding.value[1]()) {
// call method1();
}
},
unbind(el, binding, vnode) {
console.log('unbinding');
}
}
So at this point how to define the function inside the directive and use it inside the bind
and ponentUpdated
?
I have directive and I wanted to create a local method inside the directive and use it in the hook function. Here's my code:
export const OutsideClick = {
bind (el, binding, vnode) {
console.log(new Vue());
// call method1()
},
ponentUpdated(el, binding, vnode) {
console.log('updated p', binding);
if(binding.value[1]()) {
// call method1();
}
},
unbind(el, binding, vnode) {
console.log('unbinding');
}
}
So at this point how to define the function inside the directive and use it inside the bind
and ponentUpdated
?
3 Answers
Reset to default 2I don't think you can add a method inside the directive itself. But you can declare the method outside the directive and call it from inside it.
function method1 (el, binding, vnode) {
...
}
export const OutsideClick = {
bind (el, binding, vnode) {
console.log(new Vue());
method1(el, binding, vnode)
},
ponentUpdated(el, binding, vnode)
{
console.log('updated p', binding);
if(binding.value[1]()) {
method1(el, binding, vnode)
}
},
unbind(el, binding, vnode) {
console.log('unbinding')
method1(el, binding, vnode)
}
}
Well, you need to add the function outside of the directive and call it inside the lifecycle methods as per below example.
Vue.directive("color", {
"bind": function() {
console.log("directive active");
hello();
},
"unbind": function() {
console.log("directive deactive");
},
"update": function(newValue, oldValue) {
var el = $(this.el);
if (this.arg == 'background')
el.css('background', newValue);
else
el.css('color', newValue);
},
});
function hello() {
console.log('hello');
}
new Vue({
el: '#app'
});
<script src="https://cdnjs.cloudflare./ajax/libs/vue/1.0.24/vue.min.js"></script>
<script src="https://code.jquery./jquery-2.2.4.min.js"></script>
<div id="app">
<h2>Color</h2>
<select v-model="color">
<option value="#f00">Red</option>
<option value="#0f0">Green</option>
<option value="#00f">Blue</option>
<option value="#000" selected>Black</option>
</select>
<br><br>
<div v-color="color">
Hello world!!!
</div>
<h2>Background</h2>
<select v-model="color2">
<option value="#f00">Red</option>
<option value="#0f0">Green</option>
<option value="#00f">Blue</option>
<option value="#000" selected>Black</option>
</select>
<br><br>
<div v-color:background="color2">
Hello world!!!
</div>
</div>
Maybe someone will find it helpful. The trick is to wrap a directive with a constructor function.
function myDirective() {
this.myMethod = function() {
console.log('My method')
}
return {
bind: function(el) {
el.addEventListener('click', this.myMethod)
}.bind(this),
update: function() {
this.myMethod()
}.bind(this),
unbind: function(el) {
el.removeEventListener('click', this.method)
}.bind()
}
}
Vue.directive('myDirective', new myDirective())
new Vue({
el: '#app'
});
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<button v-my-directive>Just a button</button>
</div>
Also functions with .bind(this)
could be replaced with lambdas () => {}
.
本文标签: javascripthow to define a method inside the Vue directiveStack Overflow
版权声明:本文标题:javascript - how to define a method inside the Vue directive? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745537288a2662329.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论