1- import { defineComponent , type ComponentCustomOptions } from 'vue' ;
1+ import { defineComponent , type ComponentCustomOptions , type MethodOptions } from 'vue' ;
22import { obtainSlot , getSuperSlot , getProviderFunction } from './utils'
33import { build as optionSetup } from './option/setup'
44import { 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
7576type 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
0 commit comments