diff --git a/example/main.tsx b/example/main.tsx index d79bb54..5623690 100644 --- a/example/main.tsx +++ b/example/main.tsx @@ -34,7 +34,9 @@ class Child extends VueComponent { } } -@Component({ providers: [RouterStartService] }) +@Component({ + providers: [RouterStartService], +}) class App extends VueComponent { constructor(private a: RouterStartService) { super() diff --git a/src/di/index.ts b/src/di/index.ts index dc06ef4..7ccb965 100644 --- a/src/di/index.ts +++ b/src/di/index.ts @@ -96,6 +96,7 @@ export function resolveComponent(target: { new (...args: []): any }) { resolveProviders, parent, ) + console.log(11111, injector) if (options?.globalStore) { // 如果作为全局的服务,则注入到根上面 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -162,7 +163,7 @@ export function resolveDependencies(inputs: Provider[]) { export function getCurrentInjector(): ReflectiveInjector { const instance = getCurrentInstance() // @ts-ignore - return instance.provides[InjectorKey] || inject(InjectorKey) + return instance?.provides[InjectorKey] || inject(InjectorKey) } /** 手动创建当前注射器, 只能用在 setup 中 */ export function createCurrentInjector( @@ -205,4 +206,38 @@ function injectService(token: any, notFoundValue?: any) { return currentInjector.get(token, notFoundValue) } -export { injectService } +interface Constructable { + // eslint-disable-next-line @typescript-eslint/ban-types + constructor: Function +} +function provideService(...service: T[]) { + const instance = getCurrentInstance()! + // @ts-ignore + let injector: ReflectiveInjector + if (Reflect.has(instance, InjectorKey as symbol)) { + // @ts-ignore + injector = instance.provides[InjectorKey] + } + // @ts-ignore + if (!injector) { + injector = ReflectiveInjector.resolveAndCreate([], inject(InjectorKey)) + // @ts-ignore + instance.provides[InjectorKey] = injector + } + + ReflectiveInjector.resolve( + service.map((k) => ({ provide: k.constructor, useValue: k })), + ).forEach((provider, i) => { + // @ts-ignore + const index = injector._providers.length + // @ts-ignore + injector._providers[index] = provider + // @ts-ignore + injector.keyIds[index] = provider.key.id + // @ts-ignore + injector.objs[index] = provider[i] + }) + return injector +} + +export { injectService, provideService } diff --git a/src/index.ts b/src/index.ts index 49d8e1a..d13fcf2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,7 @@ export { getCurrentInjector, createCurrentInjector, injectService, + provideService, } from './di' export type { ComponentOptions } from './di' export type {