最近在重构一个旧项目,这个项目用到了一些bootstrap的插件,我们想保留这些插件,就会遇到一些问题,比如我们把插件封装成了directive,在双向绑定方面需要做如下处理:
Vue.directive("custom", {
bind: function(element, binding, vnode) {
$(element).on("click", function() {
vnode.context[binding.expression] = false; // sync binding.value = false;
});
}
});
2. <input v-model=”customValue” v-custom= “customData”></> , 我们知道v-model其实是input事件改变value的语法糖,所以我们想要在directive中对v-model进行改变,可以使用dispatchEvent(‘input’)的方法,如下例子:
// 这个函数是从vue.js源码中复制的,方便我们手动触发事件
function trigger(el, type) {
var e = document.createEvent("HTMLEvents");
e.initEvent(type, true, true);
el.dispatchEvent(e);
}
Vue.directive("custom", {
bind: function(element, binding, vnode) {
$(element).on("focus", function() {
element.value = 'FOCUS';
trigger(element, "input");
});
}
});
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有