index.js
import Popover from './src/main';import directive from './src/directive';import Vue from 'vue';// 自定义指令Vue.directive('popover', directive);/** * eg: *focus 激活 *//* istanbul ignore next */Popover.install = function (Vue) { Vue.directive('popover', directive); Vue.component(Popover.name, Popover);};Popover.directive = directive;export default Popover;
directive.js
const getReference = (el, binding, vnode) => { const _ref = binding.expression ? binding.value : binding.arg; const popper = vnode.context.$refs[_ref]; if (popper) { if (Array.isArray(popper)) { popper[0].$refs.reference = el; } else { popper.$refs.reference = el; } }};export default { // 绑定,只执行一次 bind (el, binding, vnode) { getReference(el, binding, vnode); }, // 插入父级 inserted (el, binding, vnode) { getReference(el, binding, vnode); }};
main.vue
{ { content }}