Skip to content

Commit 3d927d1

Browse files
committed
fix: 支持组件继承
1 parent 2852828 commit 3d927d1

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

example/module/basic/hello-world/a.comp.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { Mut, VueComponent } from 'vue3-oop'
1+
import { Hook, Mut, VueComponent } from 'vue3-oop'
22

33
export class A extends VueComponent {
44
@Mut() placeholder = 'dddd'
55
constructor() {
66
super()
77
}
8+
9+
@Hook('BeforeMount')
810
done() {
911
console.log('a')
1012
}

example/module/basic/hello-world/hello-world.view.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { Mut, VueComponent } from 'vue3-oop'
1+
import { Hook, Mut, VueComponent } from 'vue3-oop'
22
import { Button, Card, Input } from 'ant-design-vue'
33
import { A } from './a.comp'
44

55
export class B extends A {
6-
placeholder = 'hello wolrd'
6+
@Mut() placeholder = 'hello wolrd11'
7+
8+
@Hook('BeforeMount')
79
done() {
810
this.placeholder = Math.random().toString()
911
}
@@ -15,6 +17,7 @@ export default class HelloWorldView extends VueComponent {
1517
render() {
1618
return (
1719
<Card title={'加减功能'}>
20+
<A></A>
1821
<B></B>
1922
<Button type={'primary'} onClick={() => this.count++}>
2023
+

src/decorators/util.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@ export function createDecorator<T = void>(name: string, allowRepeat = false) {
1717
return function (target: any, key: string | symbol) {
1818
let list: MetadataStore<T | T[]>[] =
1919
Reflect.getMetadata(MetadataKey, target) || []
20+
// 处理继承
2021
list = list.slice()
21-
const hasItem = list.find((k) => k.key === key)
22-
if (!hasItem) {
22+
const hasIndex = list.findIndex((k) => k.key === key)
23+
if (hasIndex === -1) {
2324
list.push({ key, options: allowRepeat ? [options] : options })
2425
} else {
25-
if (!allowRepeat) hasItem.options = options
26-
else if (Array.isArray(hasItem.options)) hasItem.options.push(options)
26+
// 处理继承
27+
const item = Object.assign({}, list[hasIndex])
28+
if (!allowRepeat) {
29+
item.options = options
30+
} else if (Array.isArray(item.options)) {
31+
item.options = item.options.concat(options)
32+
}
33+
list[hasIndex] = item
2734
}
2835
Reflect.defineMetadata(MetadataKey, list, target)
2936
}

src/extends/component.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { LinkHandler } from '../decorators/link'
1414
import { resolveComponent } from '../di'
1515

1616
export const GlobalStoreKey = 'GlobalStoreKey'
17+
const hasOwnProperty = Object.prototype.hasOwnProperty
18+
const hasOwn = (val: any, key: string) => hasOwnProperty.call(val, key)
1719

1820
export class VueComponent<T extends {} = {}> {
1921
/** 装饰器处理 */
@@ -34,7 +36,7 @@ export class VueComponent<T extends {} = {}> {
3436
static defaultProps?: any
3537
/** vue options emits */
3638
static emits?: string[]
37-
static __vccOpts__value?: ComponentOptions & { __vccOwner?: any }
39+
static __vccOpts__value?: ComponentOptions
3840
/** 组件option定义,vue3遇到类组件会从此属性获取组件的option */
3941
static __vccOpts: ComponentOptions
4042
/** 是否作为全局store提供外部入口,此时会在 当前app上注入2个方法,用于获取此组件的服务 */
@@ -130,14 +132,14 @@ Object.defineProperty(VueComponent, '__vccOpts', {
130132
configurable: true,
131133
get() {
132134
const parentOpt = this.__vccOpts__value
133-
if (parentOpt && this === parentOpt.__vccOwner) return parentOpt
135+
if (parentOpt && hasOwn(this, '__vccOpts__value')) return parentOpt
134136
const CompConstructor = this as typeof VueComponent
135137
// eslint-disable-next-line @typescript-eslint/no-unused-vars
136138
const { displayName, defaultProps, emits, ...rest } = CompConstructor
137139

138140
// 处理继承
139141
if (parentOpt) {
140-
const mergeopt: ComponentOptions & { __vccOwner?: any } = {
142+
const mergeopt: ComponentOptions = {
141143
...parentOpt,
142144
...rest,
143145
name: displayName || CompConstructor.name,
@@ -148,7 +150,6 @@ Object.defineProperty(VueComponent, '__vccOpts', {
148150
if (CompConstructor.__vccOpts__value!.render) return instance
149151
return instance.render.bind(instance)
150152
},
151-
__vccOwner: this,
152153
}
153154
if (defaultProps) mergeopt.props = defaultProps
154155
if (emits) mergeopt.emits = emits
@@ -172,7 +173,6 @@ Object.defineProperty(VueComponent, '__vccOpts', {
172173
if (CompConstructor.__vccOpts__value!.render) return instance
173174
return instance.render.bind(instance)
174175
},
175-
__vccOwner: this,
176176
}
177177
return this.__vccOpts__value
178178
},

0 commit comments

Comments
 (0)