diff --git a/CHANGELOG.md b/CHANGELOG.md index b0609be..7f1ee0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,30 @@ +# [1.2.0](https://github.com/agileago/vue3-oop/compare/v1.1.2...v1.2.0) (2025-06-05) + + +### Bug Fixes + +* correct provides handling in provideService function ([edf85eb](https://github.com/agileago/vue3-oop/commit/edf85eb2df1bc5d32e138d5af16c61d4943e0e9a)) + + + +## [1.1.2](https://github.com/agileago/vue3-oop/compare/v1.1.1...v1.1.2) (2025-05-20) + + +### Features + +* add If component for conditional rendering ([5f8bd91](https://github.com/agileago/vue3-oop/commit/5f8bd9143aec10e7c00e568fe127fc960b73ba50)) + + + +## [1.1.1](https://github.com/agileago/vue3-oop/compare/v1.1.0...v1.1.1) (2025-05-13) + + +### Performance Improvements + +* 提高性能 ([180a8b3](https://github.com/agileago/vue3-oop/commit/180a8b3ba8819684852ef4a21c85a138c667a2d8)) + + + # [1.1.0](https://github.com/agileago/vue3-oop/compare/v1.0.21...v1.1.0) (2025-05-12) diff --git a/package.json b/package.json index ab970fc..294c266 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue3-oop", - "version": "1.1.0", + "version": "1.2.0", "packageManager": "pnpm@9.1.1", "engines": { "pnpm": ">=9.0" diff --git a/src/components/index.ts b/src/components/index.ts new file mode 100644 index 0000000..dadc791 --- /dev/null +++ b/src/components/index.ts @@ -0,0 +1,11 @@ +import type { SetupContext } from 'vue' +import type { JSX } from 'vue/jsx-runtime' + +interface IfProps { + /*判断条件*/ + condition: any +} +export function If(props: IfProps, ctx: SetupContext) { + if (!props.condition) return null + return ctx.slots.default?.() as unknown as JSX.Element +} diff --git a/src/di/index.ts b/src/di/index.ts index f27f7eb..e5ef460 100644 --- a/src/di/index.ts +++ b/src/di/index.ts @@ -170,18 +170,31 @@ interface Constructable { constructor: Function } function provideService(...service: T[]) { - const instance = getCurrentInstance()! + const currentInstance = getCurrentInstance()! + // @ts-ignore + let provides = currentInstance.provides + // by default an instance inherits its parent's provides object + // but when it needs to provide values of its own, it creates its + // own provides object using parent provides object as prototype. + // this way in `inject` we can simply look up injections from direct + // parent and let the prototype chain do the work. + // @ts-ignore + const parentProvides = currentInstance.parent && currentInstance.parent.provides + if (parentProvides === provides) { + // @ts-ignore + provides = currentInstance.provides = Object.create(parentProvides) + } // @ts-ignore let injector: ReflectiveInjector - if (Reflect.has(instance, InjectorKey as symbol)) { + if (Object.prototype.hasOwnProperty.call(provides, InjectorKey as symbol)) { // @ts-ignore - injector = instance.provides[InjectorKey] + injector = currentInstance.provides[InjectorKey] } // @ts-ignore if (!injector) { injector = ReflectiveInjector.resolveAndCreate([], inject(InjectorKey)) // @ts-ignore - instance.provides[InjectorKey] = injector + currentInstance.provides[InjectorKey] = injector } ReflectiveInjector.resolve(service.map(k => ({ provide: k.constructor, useValue: k }))).forEach((provider, i) => { diff --git a/src/index.ts b/src/index.ts index 1562404..8ac3c22 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,7 @@ export { Hook } from './decorators/hook' export { createDecorator, getProtoMetadata } from './decorators/util' export * from './helper' export * from './simple-props' +export * from './components' export { Component, InjectorKey, getCurrentInjector, createCurrentInjector, injectService, provideService } from './di' export type { ComponentOptions } from './di' export type { diff --git a/src/simple-props/composables.ts b/src/simple-props/composables.ts index daa68ec..b13f400 100644 --- a/src/simple-props/composables.ts +++ b/src/simple-props/composables.ts @@ -19,7 +19,6 @@ export function useProps(): T { const getProps = () => { return Object.fromEntries(Object.entries(instance.vnode.props || {}).map(([k, v]) => [camelizePropKey(k), v])) } - const attrs = useAttrs() return new Proxy( {}, @@ -35,7 +34,10 @@ export function useProps(): T { // @ts-ignore return instance.props[key] } else { - return attrs[key as string] || attrs[p as string] + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + instance.proxy?.$attrs + + return Reflect.get(getProps(), key, receiver) } }, ownKeys() {