Skip to content

Commit d45a6b8

Browse files
committed
+ correctly expose "methods"-option to recover compatibility to vue-class-component
1 parent bd2ade0 commit d45a6b8

File tree

7 files changed

+36
-36
lines changed

7 files changed

+36
-36
lines changed

src/component.ts

Lines changed: 12 additions & 9 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
@@ -92,11 +93,17 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
9293
}
9394
option.emits = emits
9495

96+
//apply methods
97+
let methods = Object.fromEntries(slot.obtainMap('methods'))
98+
if ('object' === typeof arg.methods && !Array.isArray(arg.methods) && arg.methods !== null) {
99+
methods = Object.assign(methods, arg.methods);
100+
}
101+
option.methods = methods
102+
95103
//merge setup function
96104
if (!option.setup) {
97105
option.setup = arg.setup
98106
} else {
99-
100107
const oldSetup: OptionSetupFunction = option.setup
101108
const newSetup: ComponentSetupFunction = arg.setup ?? function () { return {} }
102109

@@ -105,16 +112,13 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
105112
const oldRet = oldSetup(props, ctx)
106113
if (oldRet instanceof Promise || newRet instanceof Promise) {
107114
return Promise.all([newRet, oldRet]).then((arr) => {
108-
const ret = Object.assign({}, arr[0], arr[1])
109-
return ret
115+
return Object.assign({}, arr[0], arr[1])
110116
})
111117
} else {
112-
113-
const ret = Object.assign({}, newRet, oldRet)
114-
return ret
118+
return Object.assign({}, newRet, oldRet)
115119
}
116-
117120
}
121+
118122
option.setup = setup
119123
}
120124

@@ -130,7 +134,6 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
130134
if (map && map.size > 0) {
131135
map.forEach((v) => {
132136
v.forEach(ite=>ite.creator.apply({}, [option, ite.key]))
133-
134137
})
135138
}
136139

src/option/emit.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
1818
return
1919
}
2020
const emits = slot.obtainMap('emits')
21+
const methods = slot.obtainMap('methods')
2122
names.forEach((value, key) => {
2223
const eventName = value === null ? key : value
2324
emits.set(eventName, true)
24-
optionBuilder.methods![key] = async function (this: any) {
25+
26+
const emitFunction = async function (this: any) {
2527

2628
const ret = proto[key].apply(this, arguments)
2729
if (ret instanceof Promise) {
@@ -32,6 +34,7 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
3234
this.$emit(eventName, ret)
3335
}
3436
}
37+
optionBuilder.methods![key] = emitFunction
38+
methods.set(key, emitFunction)
3539
})
36-
3740
}

src/option/methodsAndHooks.ts

Lines changed: 8 additions & 20 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,46 +29,35 @@ 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)
36-
const map = slot.obtainMap('hooks')
35+
const hookMap = slot.obtainMap('hooks')
36+
const methodsMap = slot.obtainMap('methods')
3737

3838
optionBuilder.hooks ??= {}
3939
optionBuilder.methods ??= {}
4040
const HookFunctions: Record<string, Function> = {}
4141
const MethodFunctions: Record<string, Function> = {}
4242
protoArr.forEach(proto => {
4343
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
44+
return typeof des.value === 'function' && name !== 'constructor'
5345
})
5446
names = excludeNames(names, slot, (mapName) => {
5547
//include these names:
5648
//watch, user may call watch method directly
5749
//hooks, user may call hook method directly
5850
//emits, user may have a method name which is same as one of event names
59-
return !['watch', 'hooks', 'emits', 'provide'].includes(mapName)
51+
return !['methods', 'watch', 'hooks', 'emits', 'provide'].includes(mapName)
6052
});
6153
names.forEach(name => {
62-
if (HookNames.includes(name as any) || map.has(name)) {
63-
54+
if (HookNames.includes(name) || hookMap.has(name)) {
6455
HookFunctions[name] = proto[name]
65-
}
66-
else {
56+
} else {
6757
MethodFunctions[name] = proto[name]
58+
methodsMap.set(name, proto[name])
6859
}
6960
})
70-
71-
7261
})
7362

7463
Object.assign(optionBuilder.methods, MethodFunctions)
@@ -83,5 +72,4 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
8372
}
8473
}
8574
Object.assign(optionBuilder.hooks, HookFunctions)
86-
8775
}

src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export type SlotMapTypes = {
2828
ref: Map<string, RefConfig>
2929
setup: Map<string, SetupConfig>
3030
customDecorator: Map<string, CustomDecoratorRecord[]>
31+
methods: Map<string, Function>
3132
}
3233

3334
class Slot {

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)