@ylz/plugins
Version:
ylz plugins
31 lines (30 loc) • 1.14 kB
JavaScript
export default {
install: function install(Vue, options = {}) {
let directiveName = options.name || 'ref'
Vue.directive(directiveName, {
bind: function bind(el, binding, vnode) {
binding.value(vnode.componentInstance || el, vnode.key)
},
update: function update(el, binding, vnode, oldVnode) {
if (oldVnode.data && oldVnode.data.directives) {
let oldBinding = oldVnode.data.directives.find(function(directive) {
let name = directive.name
return name === directiveName
})
if (oldBinding && oldBinding.value !== binding.value) {
oldBinding && oldBinding.value(null, oldVnode.key)
binding.value(vnode.componentInstance || el, vnode.key)
return
}
}
// Should not have this situation
if (vnode.componentInstance !== oldVnode.componentInstance || vnode.elm !== oldVnode.elm) {
binding.value(vnode.componentInstance || el, vnode.key)
}
},
unbind: function unbind(el, binding, vnode) {
binding.value(null, vnode.key)
}
})
}
}