Skip to content

Commit a41aa9b

Browse files
authored
Merge pull request #87 from tall1on/master
+ correctly expose `methods` option to recover compatibility to `vue-class-component`
2 parents bd2ade0 + 891ad52 commit a41aa9b

File tree

5 files changed

+26
-33
lines changed

5 files changed

+26
-33
lines changed

src/component.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, type ComponentCustomOptions } from 'vue';
1+
import { defineComponent, type ComponentCustomOptions, type MethodOptions } from 'vue';
22
import { obtainSlot, getSuperSlot, getProviderFunction } from './utils'
33
import { build as optionSetup } from './option/setup'
44
import { build as optionComputed } from './option/computed'
@@ -70,6 +70,7 @@ type ComponentOption = {
7070
template?: string
7171
mixins?: any[]
7272
setup?: ComponentSetupFunction
73+
methods?: MethodOptions
7374
}
7475

7576
type ComponentConsOption = Cons | ComponentOption
@@ -78,7 +79,7 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
7879
const option = ComponentOption(cons, extend)
7980
const slot = obtainSlot(cons.prototype)
8081
Object.keys(arg).reduce<Record<string, any>>((option, name: string) => {
81-
if (['options', 'modifier', 'emits', 'setup', 'provide'].includes(name)) {
82+
if (['options', 'modifier', 'methods', 'emits', 'setup', 'provide'].includes(name)) {
8283
return option
8384
}
8485
option[name] = arg[name as keyof ComponentOption]
@@ -92,11 +93,16 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
9293
}
9394
option.emits = emits
9495

96+
//merge methods
97+
if ('object' === typeof arg.methods && !Array.isArray(arg.methods) && arg.methods !== null) {
98+
option.methods??={}
99+
Object.assign(option.methods, arg.methods);
100+
}
101+
95102
//merge setup function
96103
if (!option.setup) {
97104
option.setup = arg.setup
98105
} else {
99-
100106
const oldSetup: OptionSetupFunction = option.setup
101107
const newSetup: ComponentSetupFunction = arg.setup ?? function () { return {} }
102108

@@ -105,16 +111,13 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
105111
const oldRet = oldSetup(props, ctx)
106112
if (oldRet instanceof Promise || newRet instanceof Promise) {
107113
return Promise.all([newRet, oldRet]).then((arr) => {
108-
const ret = Object.assign({}, arr[0], arr[1])
109-
return ret
114+
return Object.assign({}, arr[0], arr[1])
110115
})
111116
} else {
112-
113-
const ret = Object.assign({}, newRet, oldRet)
114-
return ret
117+
return Object.assign({}, newRet, oldRet)
115118
}
116-
117119
}
120+
118121
option.setup = setup
119122
}
120123

@@ -130,7 +133,6 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
130133
if (map && map.size > 0) {
131134
map.forEach((v) => {
132135
v.forEach(ite=>ite.creator.apply({}, [option, ite.key]))
133-
134136
})
135137
}
136138

src/option/methodsAndHooks.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Cons } from '../component'
22
import type { OptionBuilder } from '../optionBuilder'
33
import { obtainSlot, toComponentReverse, excludeNames, getValidNames, optionNullableMemberDecorator } from '../utils'
44

5-
export const HookNames = [
5+
export const HookNames: ReadonlyArray<string> = [
66
"beforeCreate",
77
"created",
88
"beforeMount",
@@ -29,7 +29,6 @@ export const decorator = optionNullableMemberDecorator(function (proto: any, nam
2929
map.set(name, null)
3030
})
3131

32-
3332
export function build(cons: Cons, optionBuilder: OptionBuilder) {
3433
const slot = obtainSlot(cons.prototype)
3534
const protoArr = toComponentReverse(cons.prototype)
@@ -41,15 +40,7 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
4140
const MethodFunctions: Record<string, Function> = {}
4241
protoArr.forEach(proto => {
4342
let names = getValidNames(proto, (des, name) => {
44-
45-
if (name === 'constructor') {
46-
return false
47-
}
48-
if (typeof des.value === 'function') {
49-
50-
return true
51-
}
52-
return false
43+
return typeof des.value === 'function' && name !== 'constructor'
5344
})
5445
names = excludeNames(names, slot, (mapName) => {
5546
//include these names:
@@ -59,16 +50,12 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
5950
return !['watch', 'hooks', 'emits', 'provide'].includes(mapName)
6051
});
6152
names.forEach(name => {
62-
if (HookNames.includes(name as any) || map.has(name)) {
63-
53+
if (HookNames.includes(name) || map.has(name)) {
6454
HookFunctions[name] = proto[name]
65-
}
66-
else {
55+
} else {
6756
MethodFunctions[name] = proto[name]
6857
}
6958
})
70-
71-
7259
})
7360

7461
Object.assign(optionBuilder.methods, MethodFunctions)
@@ -83,5 +70,4 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
8370
}
8471
}
8572
Object.assign(optionBuilder.hooks, HookFunctions)
86-
8773
}

test/custom/custom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ describe('create decorator',
4040
})
4141
}
4242
)
43-
export default {}
43+
export default {}

test/option/emit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@ describe('decorator Emit',
7575
})
7676
}
7777
)
78-
export default {}
78+
export default {}

test/option/methods.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ import { expect } from 'chai';
33
import 'mocha';
44
import { Component, Base, toNative } from '../../dist'
55

6-
@Component
6+
@Component({
7+
methods: {
8+
optionTest: () => 'option value'
9+
}
10+
})
711
class Comp extends Base {
812
method() {
913
return 'method value'
1014
}
11-
1215
}
1316
const CompContext = toNative(Comp) as any
1417

@@ -17,7 +20,9 @@ describe('option methods',
1720
it('default', () => {
1821
expect('function').to.equal(typeof CompContext?.methods?.method)
1922
expect('method value').to.equal(CompContext.methods.method())
23+
expect('function').to.equal(typeof CompContext?.methods?.optionTest)
24+
expect('option value').to.equal(CompContext.methods.optionTest())
2025
})
2126
}
2227
)
23-
export default {}
28+
export default {}

0 commit comments

Comments
 (0)