vue#InjectionKey TypeScript Examples

The following examples show how to use vue#InjectionKey. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: inject.ts    From fect with MIT License 6 votes vote down vote up
useProvider = <T>(key: InjectionKey<Provider<T>>) => {
  const context = inject(key, null)
  if (context) {
    const instance = getCurrentInstance()!
    const { link, unlink, internalChildren, ...rest } = context
    link(instance)
    onUnmounted(() => unlink(instance))
    const idx = computed(() => internalChildren.indexOf(instance))
    return {
      context: rest,
      idx: idx.value
    }
  }
  return {
    context: null,
    idx: -1
  }
}
Example #2
Source File: useParent.ts    From elenext with MIT License 6 votes vote down vote up
useChildren = <T extends Record<string, unknown>>(key: InjectionKey<ParentProvide<T>>, data: T) => {
  const children: ComponentInternalInstance[] = reactive([])
  provide(key, {
    ...data,
    children,
    insert: (child: ComponentInternalInstance) => {
      children.push(child)
    },
    remove: (child: ComponentInternalInstance) => {
      const index = children.indexOf(child)
      children.splice(index, 1)
    }
  })
}
Example #3
Source File: lib.ts    From hexon with GNU General Public License v3.0 5 votes vote down vote up
notificationProviderInjectionKey: InjectionKey<Notification> =
  Symbol("notification")
Example #4
Source File: skeleton-context.ts    From fect with MIT License 5 votes vote down vote up
READONLY_SKELETON_KEY: InjectionKey<SkeletonContext> = Symbol('skeletonKey')
Example #5
Source File: FormKit.ts    From formkit with MIT License 5 votes vote down vote up
parentSymbol: InjectionKey<FormKitNode> = Symbol('FormKitParent')
Example #6
Source File: lib.ts    From hexon with GNU General Public License v3.0 5 votes vote down vote up
key: InjectionKey<IThemePlugin> = Symbol("key")
Example #7
Source File: layout-context.ts    From fect with MIT License 5 votes vote down vote up
READONLY_LAYOUT_KEY: InjectionKey<LayoutContext> = Symbol('layoutkey')
Example #8
Source File: index.ts    From vue3-ts-base with MIT License 5 votes vote down vote up
key: InjectionKey<Store<StateType>> = Symbol()
Example #9
Source File: i18n.ts    From vue-i18n-next with MIT License 5 votes vote down vote up
I18nInjectionKey: InjectionKey<I18n> | string =
  /* #__PURE__*/ makeSymbol('global-vue-i18n')
Example #10
Source File: index.ts    From Vue3-Vite-Vuetify3-Typescript-Template with MIT License 5 votes vote down vote up
key: InjectionKey<Store<storeTypes>> = Symbol()
Example #11
Source File: injection.ts    From gitmars with GNU General Public License v3.0 5 votes vote down vote up
TerminalInjectionKey: InjectionKey<TerminalInjection> =
    Symbol('Terminal')
Example #12
Source File: index.ts    From vue3-cesium-typescript-start-up-template with MIT License 5 votes vote down vote up
key: InjectionKey<Store<RootState>> = Symbol()
Example #13
Source File: Renderer.ts    From trois with MIT License 5 votes vote down vote up
RendererInjectionKey: InjectionKey<RendererPublicInterface> = Symbol('Renderer')
Example #14
Source File: avatar-context.ts    From fect with MIT License 5 votes vote down vote up
READONLY_AVATAR_KEY: InjectionKey<AvatarContext> = Symbol('avatarKey')
Example #15
Source File: config.ts    From vue-request with MIT License 5 votes vote down vote up
GLOBAL_OPTIONS_PROVIDE_KEY: InjectionKey<GlobalOptions> = Symbol(
  'GLOBAL_OPTIONS_PROVIDE_KEY',
)
Example #16
Source File: index.ts    From vueconf-london with MIT License 5 votes vote down vote up
key: InjectionKey<Store<State>> = Symbol()
Example #17
Source File: index.ts    From elenext with MIT License 5 votes vote down vote up
Breadcrumb_IJK: InjectionKey<ParentProvide<BreadcrumbProvide>> = Symbol('BreadcrumbProvide')
Example #18
Source File: usePageContext.ts    From vite-plugin-ssr with MIT License 5 votes vote down vote up
key: InjectionKey<PageContext> = Symbol()
Example #19
Source File: select-context.ts    From fect with MIT License 5 votes vote down vote up
READONLY_SELECT_KEY: InjectionKey<SelectContext> = Symbol('selectKey')
Example #20
Source File: index.ts    From css-render with MIT License 5 votes vote down vote up
ssrContextKey: InjectionKey<CssrSsrContext> = Symbol(
  '@css-render/vue3-ssr'
)
Example #21
Source File: i18n.ts    From vue-i18n-next with MIT License 4 votes vote down vote up
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
export function createI18n(options: any = {}, VueI18nLegacy?: any): any {
  type _I18n = I18n & I18nInternal

  if (__BRIDGE__) {
    _legacyVueI18n = VueI18nLegacy
  }

  // prettier-ignore
  const __legacyMode = __LITE__
    ? false
    :  __FEATURE_LEGACY_API__ && isBoolean(options.legacy)
      ? options.legacy
      : __FEATURE_LEGACY_API__
  // prettier-ignore
  const __globalInjection = isBoolean(options.globalInjection)
      ? options.globalInjection
      : true
  // prettier-ignore
  const __allowComposition = __LITE__
    ? true
    :  __FEATURE_LEGACY_API__ && __legacyMode
      ? !!options.allowComposition
      : true
  const __instances = new Map<ComponentInternalInstance, VueI18n | Composer>()
  const [globalScope, __global] = createGlobal(
    options,
    __legacyMode,
    VueI18nLegacy
  )
  const symbol: InjectionKey<I18n> | string = /* #__PURE__*/ makeSymbol(
    __DEV__ ? 'vue-i18n' : ''
  )

  function __getInstance<Instance extends VueI18n | Composer>(
    component: ComponentInternalInstance
  ): Instance | null {
    return (__instances.get(component) as unknown as Instance) || null
  }
  function __setInstance<Instance extends VueI18n | Composer>(
    component: ComponentInternalInstance,
    instance: Instance
  ): void {
    __instances.set(component, instance)
  }
  function __deleteInstance(component: ComponentInternalInstance): void {
    __instances.delete(component)
  }

  if (!__BRIDGE__) {
    const i18n = {
      // mode
      get mode(): I18nMode {
        return !__LITE__ && __FEATURE_LEGACY_API__ && __legacyMode
          ? 'legacy'
          : 'composition'
      },
      // allowComposition
      get allowComposition(): boolean {
        return __allowComposition
      },
      // install plugin
      async install(app: App, ...options: unknown[]): Promise<void> {
        if (
          !__BRIDGE__ &&
          (__DEV__ || __FEATURE_PROD_VUE_DEVTOOLS__) &&
          !__NODE_JS__
        ) {
          app.__VUE_I18N__ = i18n as unknown as _I18n
        }

        // setup global provider
        app.__VUE_I18N_SYMBOL__ = symbol
        app.provide(app.__VUE_I18N_SYMBOL__, i18n as unknown as I18n)

        // global method and properties injection for Composition API
        if (!__legacyMode && __globalInjection) {
          injectGlobalFields(app, i18n.global as Composer)
        }

        // install built-in components and directive
        if (!__LITE__ && __FEATURE_FULL_INSTALL__) {
          applyNext(app, i18n as I18n, ...options)
        }

        // setup mixin for Legacy API
        if (!__LITE__ && __FEATURE_LEGACY_API__ && __legacyMode) {
          app.mixin(
            defineMixinNext(
              __global as unknown as VueI18n,
              (__global as unknown as VueI18nInternal).__composer as Composer,
              i18n as unknown as I18nInternal
            )
          )
        }

        // release global scope
        const unmountApp = app.unmount
        app.unmount = () => {
          i18n.dispose()
          unmountApp()
        }

        // setup vue-devtools plugin
        if ((__DEV__ || __FEATURE_PROD_VUE_DEVTOOLS__) && !__NODE_JS__) {
          const ret = await enableDevTools(app, i18n as _I18n)
          if (!ret) {
            throw createI18nError(
              I18nErrorCodes.CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN
            )
          }
          const emitter: VueDevToolsEmitter =
            createEmitter<VueDevToolsEmitterEvents>()
          if (__legacyMode) {
            const _vueI18n = __global as unknown as VueI18nInternal
            _vueI18n.__enableEmitter && _vueI18n.__enableEmitter(emitter)
          } else {
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            const _composer = __global as any
            _composer[EnableEmitter] && _composer[EnableEmitter](emitter)
          }
          emitter.on('*', addTimelineEvent)
        }
      },
      // global accessor
      get global() {
        return __global
      },
      dispose(): void {
        globalScope.stop()
      },
      // @internal
      __instances,
      // @internal
      __getInstance,
      // @internal
      __setInstance,
      // @internal
      __deleteInstance
    }
    return i18n
  } else {
    // extend legacy VueI18n instance

    const i18n = (__global as any)[LegacyInstanceSymbol] // eslint-disable-line @typescript-eslint/no-explicit-any
    let _localeWatcher: Function | null = null
    Object.defineProperty(i18n, 'global', {
      get() {
        return __global
      }
    })
    Object.defineProperty(i18n, 'mode', {
      get() {
        return __legacyMode ? 'legacy' : 'composition'
      }
    })
    Object.defineProperty(i18n, 'allowComposition', {
      get() {
        return __allowComposition
      }
    })
    Object.defineProperty(i18n, '__instances', {
      get() {
        return __instances
      }
    })
    Object.defineProperty(i18n, 'install', {
      writable: true,
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      value: (Vue: any, ...options: unknown[]) => {
        const version =
          (Vue && Vue.version && Number(Vue.version.split('.')[0])) || -1
        if (version !== 2) {
          throw createI18nError(I18nErrorCodes.BRIDGE_SUPPORT_VUE_2_ONLY)
        }

        __FEATURE_FULL_INSTALL__ && applyBridge(Vue, ...options)

        if (!__legacyMode && __globalInjection) {
          _localeWatcher = injectGlobalFieldsForBridge(
            Vue,
            i18n,
            __global as Composer
          )
        }
        Vue.mixin(defineMixinBridge(i18n, _legacyVueI18n))
      }
    })
    Object.defineProperty(i18n, 'dispose', {
      value: (): void => {
        _localeWatcher && _localeWatcher()
        globalScope.stop()
      }
    })
    const methodMap = {
      __getInstance,
      __setInstance,
      __deleteInstance
    }
    Object.keys(methodMap).forEach(
      key =>
        Object.defineProperty(i18n, key, { value: (methodMap as any)[key] }) // eslint-disable-line @typescript-eslint/no-explicit-any
    )
    return i18n
  }
}