Skip to content

Instantly share code, notes, and snippets.

@alanmshelly
Last active January 2, 2024 18:21
Show Gist options
  • Save alanmshelly/b3fe89dbbd994242ed637aa981a6b668 to your computer and use it in GitHub Desktop.
Save alanmshelly/b3fe89dbbd994242ed637aa981a6b668 to your computer and use it in GitHub Desktop.
Vue.js Higher Order Component example in Typescript
interface Dependencies {
dependency: Dependency
}
export default function withDependenciesProvided(
WrappedComponent: VueConstructor,
dependenciesToProvide: Dependencies,
) {
return Vue.extend({
provide() {
return dependenciesToProvide
},
render(h: CreateElement): VNode {
return h(WrappedComponent, {
on: this.$listeners,
attrs: this.$attrs,
scopedSlots: this.$scopedSlots,
})
},
})
}
// To use
const dependency = new Dependency()
export default {
name: 'App',
components: {
SomeView: withDependenciesProvided(SomeView, {dependency})
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment