Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

registrable.ts 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import Vue from 'vue'
  2. import { VueConstructor } from 'vue/types/vue'
  3. import { consoleWarn } from '../util/console'
  4. function generateWarning (child: string, parent: string) {
  5. return () => consoleWarn(`The ${child} component must be used inside a ${parent}`)
  6. }
  7. export type Registrable<T extends string> = VueConstructor<Vue & {
  8. [K in T]: {
  9. register (...props: any[]): void
  10. unregister (self: any): void
  11. }
  12. }>
  13. export function inject<T extends string> (namespace: T, child?: string, parent?: string): Registrable<T> {
  14. const defaultImpl = child && parent ? {
  15. register: generateWarning(child, parent),
  16. unregister: generateWarning(child, parent)
  17. } : null
  18. return Vue.extend({
  19. name: 'registrable-inject',
  20. inject: {
  21. [namespace]: {
  22. default: defaultImpl
  23. }
  24. }
  25. })
  26. }
  27. export function provide (namespace: string) {
  28. return Vue.extend({
  29. name: 'registrable-provide',
  30. methods: {
  31. register: null,
  32. unregister: null
  33. },
  34. provide () {
  35. return {
  36. [namespace]: {
  37. register: this.register,
  38. unregister: this.unregister
  39. }
  40. }
  41. }
  42. })
  43. }