好吧,第一次听说Vue的Hoc:高阶组件。
看了一些案例,发现下面这个具有代表性:
//Parent.vue import Base from ‘.Base.vue‘; // 使用hoc替代minxin import HOC from ‘.hoc‘; const wrapBase = HOC(Base); export default { components: { Base, wrapBase }, //... }
HOC的代码
//完整hoc.js export default function HOC(Base) { return { mounted () { console.log(‘我是HOC mounted log‘) }, props: Base.props, render (h) { const slots = Object.keys(this.$slots) .reduce((acc, cur) => acc.concat(this.$slots[cur]), []) // 手动更正 context .map(vnode => { vnode.context = this._self //绑定到高阶组件上 return vnode }) return h(Base, { on: this.$listeners, props: this.$props, attrs: this.$attrs }, slots) } } }
HOC可以实现对部分组件的全局拦截。
详情:https://zhuanlan.zhihu.com/p/181673485
原文:https://www.cnblogs.com/zhensg123/p/14659767.html