2019-09-04 10:25:38 +08:00
|
|
|
import {
|
2019-09-07 00:58:31 +08:00
|
|
|
ComponentInternalInstance,
|
2019-09-04 10:25:38 +08:00
|
|
|
Data,
|
2019-10-22 01:44:01 +08:00
|
|
|
SetupContext,
|
2020-06-09 23:27:40 +08:00
|
|
|
ComponentInternalOptions,
|
2020-07-28 05:44:17 +08:00
|
|
|
Component,
|
2020-08-20 04:11:29 +08:00
|
|
|
ConcreteComponent,
|
2020-10-08 04:58:19 +08:00
|
|
|
InternalRenderFunction,
|
|
|
|
LifecycleHooks
|
2019-09-04 10:25:38 +08:00
|
|
|
} from './component'
|
|
|
|
import {
|
|
|
|
isFunction,
|
|
|
|
extend,
|
|
|
|
isString,
|
|
|
|
isObject,
|
|
|
|
isArray,
|
2020-03-24 23:59:00 +08:00
|
|
|
NOOP,
|
2022-01-16 15:43:19 +08:00
|
|
|
isPromise,
|
|
|
|
LooseRequired,
|
|
|
|
UnionToIntersection
|
2019-09-04 10:25:38 +08:00
|
|
|
} from '@vue/shared'
|
2022-01-21 12:31:54 +08:00
|
|
|
import { isRef, Ref } from '@vue/reactivity'
|
|
|
|
import { computed } from './apiComputed'
|
2021-04-08 04:19:04 +08:00
|
|
|
import {
|
|
|
|
watch,
|
|
|
|
WatchOptions,
|
|
|
|
WatchCallback,
|
|
|
|
createPathGetter
|
|
|
|
} from './apiWatch'
|
2019-09-04 10:25:38 +08:00
|
|
|
import { provide, inject } from './apiInject'
|
|
|
|
import {
|
|
|
|
onBeforeMount,
|
|
|
|
onMounted,
|
|
|
|
onBeforeUpdate,
|
|
|
|
onUpdated,
|
|
|
|
onErrorCaptured,
|
|
|
|
onRenderTracked,
|
|
|
|
onBeforeUnmount,
|
2019-10-02 21:46:29 +08:00
|
|
|
onUnmounted,
|
2019-10-30 10:28:38 +08:00
|
|
|
onActivated,
|
|
|
|
onDeactivated,
|
2019-10-15 11:15:09 +08:00
|
|
|
onRenderTriggered,
|
|
|
|
DebuggerHook,
|
2021-05-08 00:00:52 +08:00
|
|
|
ErrorCapturedHook,
|
|
|
|
onServerPrefetch
|
2019-09-04 10:25:38 +08:00
|
|
|
} from './apiLifecycle'
|
2019-10-22 01:57:20 +08:00
|
|
|
import {
|
|
|
|
reactive,
|
|
|
|
ComputedGetter,
|
2021-06-25 09:28:09 +08:00
|
|
|
WritableComputedOptions
|
2019-10-22 01:57:20 +08:00
|
|
|
} from '@vue/reactivity'
|
2020-09-16 22:09:35 +08:00
|
|
|
import {
|
|
|
|
ComponentObjectPropsOptions,
|
|
|
|
ExtractPropTypes,
|
|
|
|
ExtractDefaultPropTypes
|
|
|
|
} from './componentProps'
|
2021-07-20 06:22:19 +08:00
|
|
|
import { EmitsOptions, EmitsToProps } from './componentEmits'
|
2019-09-06 23:19:22 +08:00
|
|
|
import { Directive } from './directives'
|
2020-06-09 22:37:00 +08:00
|
|
|
import {
|
|
|
|
CreateComponentPublicInstance,
|
2022-05-13 16:52:19 +08:00
|
|
|
ComponentPublicInstance,
|
|
|
|
isReservedPrefix
|
2020-08-20 04:11:29 +08:00
|
|
|
} from './componentPublicInstance'
|
2019-10-02 22:03:43 +08:00
|
|
|
import { warn } from './warning'
|
2020-05-01 22:37:40 +08:00
|
|
|
import { VNodeChild } from './vnode'
|
2020-10-08 04:58:19 +08:00
|
|
|
import { callWithAsyncErrorHandling } from './errorHandling'
|
2021-04-06 21:31:47 +08:00
|
|
|
import { deepMergeData } from './compat/data'
|
2021-04-13 06:47:50 +08:00
|
|
|
import { DeprecationTypes } from './compat/compatConfig'
|
2021-04-09 05:11:05 +08:00
|
|
|
import {
|
|
|
|
CompatConfig,
|
|
|
|
isCompatEnabled,
|
|
|
|
softAssertCompatEnabled
|
|
|
|
} from './compat/compatConfig'
|
2021-06-02 22:37:50 +08:00
|
|
|
import { OptionMergeFunction } from './apiCreateApp'
|
2019-09-06 23:19:22 +08:00
|
|
|
|
2020-04-17 21:41:36 +08:00
|
|
|
/**
|
|
|
|
* Interface for declaring custom options.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* declare module '@vue/runtime-core' {
|
|
|
|
* interface ComponentCustomOptions {
|
|
|
|
* beforeRouteUpdate?(
|
|
|
|
* to: Route,
|
|
|
|
* from: Route,
|
|
|
|
* next: () => void
|
|
|
|
* ): void
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export interface ComponentCustomOptions {}
|
|
|
|
|
2020-05-01 22:37:40 +08:00
|
|
|
export type RenderFunction = () => VNodeChild
|
|
|
|
|
2020-12-05 05:06:34 +08:00
|
|
|
type ExtractOptionProp<T> = T extends ComponentOptionsBase<
|
2021-07-20 06:22:19 +08:00
|
|
|
infer P, // Props
|
|
|
|
any, // RawBindings
|
|
|
|
any, // D
|
|
|
|
any, // C
|
|
|
|
any, // M
|
|
|
|
any, // Mixin
|
|
|
|
any, // Extends
|
|
|
|
any // EmitsOptions
|
2020-12-05 05:06:34 +08:00
|
|
|
>
|
2021-07-20 06:22:19 +08:00
|
|
|
? unknown extends P
|
|
|
|
? {}
|
|
|
|
: P
|
2020-12-05 05:06:34 +08:00
|
|
|
: {}
|
|
|
|
|
2019-10-22 23:26:48 +08:00
|
|
|
export interface ComponentOptionsBase<
|
2019-09-06 23:19:22 +08:00
|
|
|
Props,
|
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C extends ComputedOptions,
|
2020-04-04 00:05:52 +08:00
|
|
|
M extends MethodOptions,
|
2020-06-09 22:37:00 +08:00
|
|
|
Mixin extends ComponentOptionsMixin,
|
|
|
|
Extends extends ComponentOptionsMixin,
|
2020-04-04 00:05:52 +08:00
|
|
|
E extends EmitsOptions,
|
2020-09-16 22:09:35 +08:00
|
|
|
EE extends string = string,
|
2022-05-06 17:44:05 +08:00
|
|
|
Defaults = {},
|
|
|
|
Provide extends ComponentProvideOptions = ComponentProvideOptions
|
|
|
|
> extends LegacyOptions<Props, D, C, M, Mixin, Extends, Provide>,
|
2020-06-09 23:27:40 +08:00
|
|
|
ComponentInternalOptions,
|
2020-04-17 21:41:36 +08:00
|
|
|
ComponentCustomOptions {
|
2019-09-06 23:19:22 +08:00
|
|
|
setup?: (
|
2020-01-11 00:46:34 +08:00
|
|
|
this: void,
|
2021-05-13 05:47:31 +08:00
|
|
|
props: Readonly<
|
|
|
|
LooseRequired<
|
|
|
|
Props &
|
|
|
|
UnionToIntersection<ExtractOptionProp<Mixin>> &
|
|
|
|
UnionToIntersection<ExtractOptionProp<Extends>>
|
|
|
|
>
|
|
|
|
>,
|
2020-11-25 04:12:59 +08:00
|
|
|
ctx: SetupContext<E>
|
2020-09-15 23:46:11 +08:00
|
|
|
) => Promise<RawBindings> | RawBindings | RenderFunction | void
|
2019-09-06 23:19:22 +08:00
|
|
|
name?: string
|
2019-12-11 23:39:29 +08:00
|
|
|
template?: string | object // can be a direct DOM node
|
2019-09-06 23:19:22 +08:00
|
|
|
// Note: we are intentionally using the signature-less `Function` type here
|
|
|
|
// since any type with signature will cause the whole inference to fail when
|
|
|
|
// the return expression contains reference to `this`.
|
|
|
|
// Luckily `render()` doesn't need any arguments nor does it care about return
|
|
|
|
// type.
|
|
|
|
render?: Function
|
2020-08-20 04:11:29 +08:00
|
|
|
components?: Record<string, Component>
|
2019-09-06 23:19:22 +08:00
|
|
|
directives?: Record<string, Directive>
|
2019-10-26 00:12:17 +08:00
|
|
|
inheritAttrs?: boolean
|
2020-08-15 05:36:26 +08:00
|
|
|
emits?: (E | EE[]) & ThisType<void>
|
2020-11-15 01:49:35 +08:00
|
|
|
// TODO infer public instance type based on exposed keys
|
|
|
|
expose?: string[]
|
2020-09-02 10:52:46 +08:00
|
|
|
serverPrefetch?(): Promise<any>
|
2019-11-01 00:43:05 +08:00
|
|
|
|
2021-04-28 23:35:59 +08:00
|
|
|
// Runtime compiler only -----------------------------------------------------
|
|
|
|
compilerOptions?: RuntimeCompilerOptions
|
|
|
|
|
2020-03-24 23:59:00 +08:00
|
|
|
// Internal ------------------------------------------------------------------
|
|
|
|
|
2020-05-01 22:37:40 +08:00
|
|
|
/**
|
|
|
|
* SSR only. This is produced by compiler-ssr and attached in compiler-sfc
|
|
|
|
* not user facing, so the typing is lax and for test only.
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
ssrRender?: (
|
|
|
|
ctx: any,
|
|
|
|
push: (item: any) => void,
|
2020-06-27 02:23:50 +08:00
|
|
|
parentInstance: ComponentInternalInstance,
|
2020-07-11 10:12:25 +08:00
|
|
|
attrs: Data | undefined,
|
|
|
|
// for compiler-optimized bindings
|
|
|
|
$props: ComponentInternalInstance['props'],
|
|
|
|
$setup: ComponentInternalInstance['setupState'],
|
|
|
|
$data: ComponentInternalInstance['data'],
|
|
|
|
$options: ComponentInternalInstance['ctx']
|
2020-05-01 22:37:40 +08:00
|
|
|
) => void
|
|
|
|
|
2020-11-21 03:22:51 +08:00
|
|
|
/**
|
|
|
|
* Only generated by compiler-sfc to mark a ssr render function inlined and
|
|
|
|
* returned from setup()
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
__ssrInlineRender?: boolean
|
|
|
|
|
2020-05-01 22:37:40 +08:00
|
|
|
/**
|
|
|
|
* marker for AsyncComponentWrapper
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-08-20 04:11:29 +08:00
|
|
|
__asyncLoader?: () => Promise<ConcreteComponent>
|
2021-05-25 22:50:00 +08:00
|
|
|
/**
|
|
|
|
* the inner component resolved by the AsyncComponentWrapper
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
__asyncResolved?: ConcreteComponent
|
2020-03-24 23:59:00 +08:00
|
|
|
|
2020-05-01 22:37:40 +08:00
|
|
|
// Type differentiators ------------------------------------------------------
|
|
|
|
|
|
|
|
// Note these are internal but need to be exposed in d.ts for type inference
|
|
|
|
// to work!
|
|
|
|
|
2019-11-28 17:01:53 +08:00
|
|
|
// type-only differentiator to separate OptionWithoutProps from a constructor
|
2019-12-22 23:58:12 +08:00
|
|
|
// type returned by defineComponent() or FunctionalComponent
|
2020-06-09 22:17:42 +08:00
|
|
|
call?: (this: unknown, ...args: unknown[]) => never
|
2019-11-02 10:54:01 +08:00
|
|
|
// type-only differentiators for built-in Vnode types
|
|
|
|
__isFragment?: never
|
2020-03-31 22:52:42 +08:00
|
|
|
__isTeleport?: never
|
2019-11-02 10:54:01 +08:00
|
|
|
__isSuspense?: never
|
2020-09-16 22:09:35 +08:00
|
|
|
|
|
|
|
__defaults?: Defaults
|
2019-09-06 23:19:22 +08:00
|
|
|
}
|
|
|
|
|
2021-04-28 23:35:59 +08:00
|
|
|
/**
|
|
|
|
* Subset of compiler options that makes sense for the runtime.
|
|
|
|
*/
|
|
|
|
export interface RuntimeCompilerOptions {
|
|
|
|
isCustomElement?: (tag: string) => boolean
|
|
|
|
whitespace?: 'preserve' | 'condense'
|
|
|
|
comments?: boolean
|
|
|
|
delimiters?: [string, string]
|
|
|
|
}
|
|
|
|
|
2019-09-06 23:19:22 +08:00
|
|
|
export type ComponentOptionsWithoutProps<
|
|
|
|
Props = {},
|
|
|
|
RawBindings = {},
|
|
|
|
D = {},
|
|
|
|
C extends ComputedOptions = {},
|
2020-04-04 00:05:52 +08:00
|
|
|
M extends MethodOptions = {},
|
2020-06-09 22:37:00 +08:00
|
|
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
|
|
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
|
2020-04-04 08:40:34 +08:00
|
|
|
E extends EmitsOptions = EmitsOptions,
|
2021-07-20 06:22:19 +08:00
|
|
|
EE extends string = string,
|
2022-05-06 17:44:05 +08:00
|
|
|
Provide extends ComponentProvideOptions = ComponentProvideOptions,
|
2021-07-20 06:22:19 +08:00
|
|
|
PE = Props & EmitsToProps<E>
|
2020-09-16 22:09:35 +08:00
|
|
|
> = ComponentOptionsBase<
|
2021-07-20 06:22:19 +08:00
|
|
|
PE,
|
2020-09-16 22:09:35 +08:00
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C,
|
|
|
|
M,
|
|
|
|
Mixin,
|
|
|
|
Extends,
|
|
|
|
E,
|
|
|
|
EE,
|
2022-05-06 17:44:05 +08:00
|
|
|
{},
|
|
|
|
Provide
|
2020-09-16 22:09:35 +08:00
|
|
|
> & {
|
2019-09-06 23:19:22 +08:00
|
|
|
props?: undefined
|
2020-04-04 00:05:52 +08:00
|
|
|
} & ThisType<
|
2021-07-20 06:22:19 +08:00
|
|
|
CreateComponentPublicInstance<PE, RawBindings, D, C, M, Mixin, Extends, E>
|
2020-04-04 00:05:52 +08:00
|
|
|
>
|
2019-09-06 23:19:22 +08:00
|
|
|
|
|
|
|
export type ComponentOptionsWithArrayProps<
|
|
|
|
PropNames extends string = string,
|
|
|
|
RawBindings = {},
|
|
|
|
D = {},
|
|
|
|
C extends ComputedOptions = {},
|
|
|
|
M extends MethodOptions = {},
|
2020-06-09 22:37:00 +08:00
|
|
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
|
|
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
|
2020-04-04 08:40:34 +08:00
|
|
|
E extends EmitsOptions = EmitsOptions,
|
2020-04-04 00:05:52 +08:00
|
|
|
EE extends string = string,
|
2022-05-06 17:44:05 +08:00
|
|
|
Provide extends ComponentProvideOptions = ComponentProvideOptions,
|
2021-07-20 06:22:19 +08:00
|
|
|
Props = Readonly<{ [key in PropNames]?: any }> & EmitsToProps<E>
|
2020-09-16 22:09:35 +08:00
|
|
|
> = ComponentOptionsBase<
|
|
|
|
Props,
|
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C,
|
|
|
|
M,
|
|
|
|
Mixin,
|
|
|
|
Extends,
|
|
|
|
E,
|
|
|
|
EE,
|
2022-05-06 17:44:05 +08:00
|
|
|
{},
|
|
|
|
Provide
|
2020-09-16 22:09:35 +08:00
|
|
|
> & {
|
2019-09-06 23:19:22 +08:00
|
|
|
props: PropNames[]
|
2020-06-09 22:37:00 +08:00
|
|
|
} & ThisType<
|
|
|
|
CreateComponentPublicInstance<
|
|
|
|
Props,
|
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C,
|
|
|
|
M,
|
|
|
|
Mixin,
|
|
|
|
Extends,
|
|
|
|
E
|
|
|
|
>
|
|
|
|
>
|
2019-09-06 23:19:22 +08:00
|
|
|
|
2019-10-08 21:26:09 +08:00
|
|
|
export type ComponentOptionsWithObjectProps<
|
|
|
|
PropsOptions = ComponentObjectPropsOptions,
|
2019-09-06 23:19:22 +08:00
|
|
|
RawBindings = {},
|
|
|
|
D = {},
|
|
|
|
C extends ComputedOptions = {},
|
|
|
|
M extends MethodOptions = {},
|
2020-06-09 22:37:00 +08:00
|
|
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
|
|
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
|
2020-04-04 08:40:34 +08:00
|
|
|
E extends EmitsOptions = EmitsOptions,
|
2020-04-04 00:05:52 +08:00
|
|
|
EE extends string = string,
|
2022-05-06 17:44:05 +08:00
|
|
|
Provide extends ComponentProvideOptions = ComponentProvideOptions,
|
2021-07-20 06:22:19 +08:00
|
|
|
Props = Readonly<ExtractPropTypes<PropsOptions>> & EmitsToProps<E>,
|
2020-09-16 22:09:35 +08:00
|
|
|
Defaults = ExtractDefaultPropTypes<PropsOptions>
|
|
|
|
> = ComponentOptionsBase<
|
|
|
|
Props,
|
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C,
|
|
|
|
M,
|
|
|
|
Mixin,
|
|
|
|
Extends,
|
|
|
|
E,
|
|
|
|
EE,
|
2022-05-06 17:44:05 +08:00
|
|
|
Defaults,
|
|
|
|
Provide
|
2020-09-16 22:09:35 +08:00
|
|
|
> & {
|
2020-08-18 23:25:55 +08:00
|
|
|
props: PropsOptions & ThisType<void>
|
2020-06-09 22:37:00 +08:00
|
|
|
} & ThisType<
|
|
|
|
CreateComponentPublicInstance<
|
|
|
|
Props,
|
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C,
|
|
|
|
M,
|
|
|
|
Mixin,
|
|
|
|
Extends,
|
2020-09-16 22:09:35 +08:00
|
|
|
E,
|
|
|
|
Props,
|
|
|
|
Defaults,
|
|
|
|
false
|
2020-06-09 22:37:00 +08:00
|
|
|
>
|
|
|
|
>
|
2019-09-06 23:19:22 +08:00
|
|
|
|
2020-09-15 23:46:11 +08:00
|
|
|
export type ComponentOptions<
|
|
|
|
Props = {},
|
|
|
|
RawBindings = any,
|
|
|
|
D = any,
|
|
|
|
C extends ComputedOptions = any,
|
|
|
|
M extends MethodOptions = any,
|
|
|
|
Mixin extends ComponentOptionsMixin = any,
|
|
|
|
Extends extends ComponentOptionsMixin = any,
|
|
|
|
E extends EmitsOptions = any
|
|
|
|
> = ComponentOptionsBase<Props, RawBindings, D, C, M, Mixin, Extends, E> &
|
|
|
|
ThisType<
|
|
|
|
CreateComponentPublicInstance<
|
|
|
|
{},
|
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C,
|
|
|
|
M,
|
|
|
|
Mixin,
|
|
|
|
Extends,
|
|
|
|
E,
|
|
|
|
Readonly<Props>
|
|
|
|
>
|
|
|
|
>
|
2019-09-04 10:25:38 +08:00
|
|
|
|
2020-06-09 22:37:00 +08:00
|
|
|
export type ComponentOptionsMixin = ComponentOptionsBase<
|
|
|
|
any,
|
|
|
|
any,
|
|
|
|
any,
|
|
|
|
any,
|
|
|
|
any,
|
|
|
|
any,
|
|
|
|
any,
|
|
|
|
any,
|
2020-09-16 22:09:35 +08:00
|
|
|
any,
|
2020-06-09 22:37:00 +08:00
|
|
|
any
|
|
|
|
>
|
|
|
|
|
2019-10-22 01:57:20 +08:00
|
|
|
export type ComputedOptions = Record<
|
|
|
|
string,
|
|
|
|
ComputedGetter<any> | WritableComputedOptions<any>
|
|
|
|
>
|
2019-09-06 04:09:30 +08:00
|
|
|
|
|
|
|
export interface MethodOptions {
|
|
|
|
[key: string]: Function
|
|
|
|
}
|
|
|
|
|
2019-10-05 22:38:02 +08:00
|
|
|
export type ExtractComputedReturns<T extends any> = {
|
2020-06-09 22:17:42 +08:00
|
|
|
[key in keyof T]: T[key] extends { get: (...args: any[]) => infer TReturn }
|
|
|
|
? TReturn
|
2021-07-20 06:22:19 +08:00
|
|
|
: T[key] extends (...args: any[]) => infer TReturn
|
|
|
|
? TReturn
|
|
|
|
: never
|
2019-09-06 04:09:30 +08:00
|
|
|
}
|
|
|
|
|
2021-04-22 21:49:25 +08:00
|
|
|
export type ObjectWatchOptionItem = {
|
|
|
|
handler: WatchCallback | string
|
|
|
|
} & WatchOptions
|
|
|
|
|
|
|
|
type WatchOptionItem = string | WatchCallback | ObjectWatchOptionItem
|
2019-10-25 22:25:52 +08:00
|
|
|
|
|
|
|
type ComponentWatchOptionItem = WatchOptionItem | WatchOptionItem[]
|
|
|
|
|
|
|
|
type ComponentWatchOptions = Record<string, ComponentWatchOptionItem>
|
2019-09-06 23:19:22 +08:00
|
|
|
|
2022-05-06 17:44:05 +08:00
|
|
|
export type ComponentProvideOptions = ObjectProvideOptions | Function
|
|
|
|
|
|
|
|
type ObjectProvideOptions = Record<string | symbol, unknown>
|
|
|
|
|
2021-06-03 02:37:27 +08:00
|
|
|
type ComponentInjectOptions = string[] | ObjectInjectOptions
|
|
|
|
|
|
|
|
type ObjectInjectOptions = Record<
|
|
|
|
string | symbol,
|
|
|
|
string | symbol | { from?: string | symbol; default?: unknown }
|
|
|
|
>
|
2019-09-06 23:19:22 +08:00
|
|
|
|
2020-05-06 23:07:11 +08:00
|
|
|
interface LegacyOptions<
|
2019-09-06 04:09:30 +08:00
|
|
|
Props,
|
|
|
|
D,
|
|
|
|
C extends ComputedOptions,
|
2020-06-09 22:37:00 +08:00
|
|
|
M extends MethodOptions,
|
|
|
|
Mixin extends ComponentOptionsMixin,
|
2022-05-06 17:44:05 +08:00
|
|
|
Extends extends ComponentOptionsMixin,
|
|
|
|
Provide extends ComponentProvideOptions = ComponentProvideOptions
|
2019-09-06 04:09:30 +08:00
|
|
|
> {
|
2021-04-09 05:11:05 +08:00
|
|
|
compatConfig?: CompatConfig
|
|
|
|
|
2020-03-24 23:59:00 +08:00
|
|
|
// allow any custom options
|
|
|
|
[key: string]: any
|
2019-09-04 10:25:38 +08:00
|
|
|
|
|
|
|
// state
|
2019-09-06 23:19:22 +08:00
|
|
|
// Limitation: we cannot expose RawBindings on the `this` context for data
|
|
|
|
// since that leads to some sort of circular inference and breaks ThisType
|
|
|
|
// for the entire component.
|
2020-03-23 23:08:22 +08:00
|
|
|
data?: (
|
2020-12-05 05:06:34 +08:00
|
|
|
this: CreateComponentPublicInstance<
|
|
|
|
Props,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
MethodOptions,
|
|
|
|
Mixin,
|
|
|
|
Extends
|
|
|
|
>,
|
|
|
|
vm: CreateComponentPublicInstance<
|
|
|
|
Props,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
MethodOptions,
|
|
|
|
Mixin,
|
|
|
|
Extends
|
|
|
|
>
|
2020-03-23 23:08:22 +08:00
|
|
|
) => D
|
2019-09-06 06:48:49 +08:00
|
|
|
computed?: C
|
|
|
|
methods?: M
|
2019-09-06 23:19:22 +08:00
|
|
|
watch?: ComponentWatchOptions
|
2022-05-06 17:44:05 +08:00
|
|
|
provide?: Provide
|
2019-09-06 23:19:22 +08:00
|
|
|
inject?: ComponentInjectOptions
|
2019-09-04 10:25:38 +08:00
|
|
|
|
2021-04-20 00:08:26 +08:00
|
|
|
// assets
|
|
|
|
filters?: Record<string, Function>
|
|
|
|
|
2019-09-04 10:25:38 +08:00
|
|
|
// composition
|
2020-06-09 22:37:00 +08:00
|
|
|
mixins?: Mixin[]
|
|
|
|
extends?: Extends
|
2019-09-04 10:25:38 +08:00
|
|
|
|
|
|
|
// lifecycle
|
2019-09-06 06:48:49 +08:00
|
|
|
beforeCreate?(): void
|
|
|
|
created?(): void
|
2019-09-04 10:25:38 +08:00
|
|
|
beforeMount?(): void
|
|
|
|
mounted?(): void
|
|
|
|
beforeUpdate?(): void
|
|
|
|
updated?(): void
|
|
|
|
activated?(): void
|
2019-10-05 22:48:54 +08:00
|
|
|
deactivated?(): void
|
2020-09-15 09:47:58 +08:00
|
|
|
/** @deprecated use `beforeUnmount` instead */
|
|
|
|
beforeDestroy?(): void
|
2019-09-05 22:07:43 +08:00
|
|
|
beforeUnmount?(): void
|
2020-09-15 09:47:58 +08:00
|
|
|
/** @deprecated use `unmounted` instead */
|
|
|
|
destroyed?(): void
|
2019-09-05 22:07:43 +08:00
|
|
|
unmounted?(): void
|
2019-10-15 11:15:09 +08:00
|
|
|
renderTracked?: DebuggerHook
|
|
|
|
renderTriggered?: DebuggerHook
|
|
|
|
errorCaptured?: ErrorCapturedHook
|
2020-07-23 09:29:59 +08:00
|
|
|
|
2021-04-28 23:35:59 +08:00
|
|
|
/**
|
|
|
|
* runtime compile only
|
|
|
|
* @deprecated use `compilerOptions.delimiters` instead.
|
|
|
|
*/
|
2020-07-23 09:29:59 +08:00
|
|
|
delimiters?: [string, string]
|
2021-03-30 06:07:36 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* #3468
|
|
|
|
*
|
|
|
|
* type-only, used to assist Mixin's type inference,
|
|
|
|
* typescript will try to simplify the inferred `Mixin` type,
|
2022-05-10 10:51:51 +08:00
|
|
|
* with the `__differentiator`, typescript won't be able to combine different mixins,
|
|
|
|
* because the `__differentiator` will be different
|
2021-03-30 06:07:36 +08:00
|
|
|
*/
|
|
|
|
__differentiator?: keyof D | keyof C | keyof M
|
2019-09-04 10:25:38 +08:00
|
|
|
}
|
|
|
|
|
2021-07-20 06:22:19 +08:00
|
|
|
type MergedHook<T = () => void> = T | T[]
|
2021-06-02 22:37:50 +08:00
|
|
|
|
2021-06-03 02:37:27 +08:00
|
|
|
export type MergedComponentOptions = ComponentOptions &
|
|
|
|
MergedComponentOptionsOverride
|
|
|
|
|
2021-06-02 22:37:50 +08:00
|
|
|
export type MergedComponentOptionsOverride = {
|
|
|
|
beforeCreate?: MergedHook
|
|
|
|
created?: MergedHook
|
|
|
|
beforeMount?: MergedHook
|
|
|
|
mounted?: MergedHook
|
|
|
|
beforeUpdate?: MergedHook
|
|
|
|
updated?: MergedHook
|
|
|
|
activated?: MergedHook
|
|
|
|
deactivated?: MergedHook
|
|
|
|
/** @deprecated use `beforeUnmount` instead */
|
|
|
|
beforeDestroy?: MergedHook
|
|
|
|
beforeUnmount?: MergedHook
|
|
|
|
/** @deprecated use `unmounted` instead */
|
|
|
|
destroyed?: MergedHook
|
|
|
|
unmounted?: MergedHook
|
|
|
|
renderTracked?: MergedHook<DebuggerHook>
|
|
|
|
renderTriggered?: MergedHook<DebuggerHook>
|
|
|
|
errorCaptured?: MergedHook<ErrorCapturedHook>
|
|
|
|
}
|
|
|
|
|
2020-09-16 22:09:35 +08:00
|
|
|
export type OptionTypesKeys = 'P' | 'B' | 'D' | 'C' | 'M' | 'Defaults'
|
2020-06-09 22:37:00 +08:00
|
|
|
|
|
|
|
export type OptionTypesType<
|
|
|
|
P = {},
|
|
|
|
B = {},
|
|
|
|
D = {},
|
|
|
|
C extends ComputedOptions = {},
|
2020-09-16 22:09:35 +08:00
|
|
|
M extends MethodOptions = {},
|
|
|
|
Defaults = {}
|
2020-06-09 22:37:00 +08:00
|
|
|
> = {
|
|
|
|
P: P
|
|
|
|
B: B
|
|
|
|
D: D
|
|
|
|
C: C
|
|
|
|
M: M
|
2020-09-16 22:09:35 +08:00
|
|
|
Defaults: Defaults
|
2020-06-09 22:37:00 +08:00
|
|
|
}
|
|
|
|
|
2019-10-22 11:47:16 +08:00
|
|
|
const enum OptionTypes {
|
|
|
|
PROPS = 'Props',
|
|
|
|
DATA = 'Data',
|
|
|
|
COMPUTED = 'Computed',
|
|
|
|
METHODS = 'Methods',
|
|
|
|
INJECT = 'Inject'
|
|
|
|
}
|
|
|
|
|
|
|
|
function createDuplicateChecker() {
|
|
|
|
const cache = Object.create(null)
|
|
|
|
return (type: OptionTypes, key: string) => {
|
|
|
|
if (cache[key]) {
|
|
|
|
warn(`${type} property "${key}" is already defined in ${cache[key]}.`)
|
|
|
|
} else {
|
|
|
|
cache[key] = type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 03:48:12 +08:00
|
|
|
export let shouldCacheAccess = true
|
2020-08-04 05:04:45 +08:00
|
|
|
|
2021-06-03 02:37:27 +08:00
|
|
|
export function applyOptions(instance: ComponentInternalInstance) {
|
|
|
|
const options = resolveMergedOptions(instance)
|
2021-06-25 09:28:09 +08:00
|
|
|
const publicThis = instance.proxy! as any
|
2021-06-03 02:37:27 +08:00
|
|
|
const ctx = instance.ctx
|
|
|
|
|
|
|
|
// do not cache property access on public proxy during state initialization
|
|
|
|
shouldCacheAccess = false
|
|
|
|
|
|
|
|
// call beforeCreate first before accessing other options since
|
|
|
|
// the hook may mutate resolved options (#2791)
|
|
|
|
if (options.beforeCreate) {
|
|
|
|
callHook(options.beforeCreate, instance, LifecycleHooks.BEFORE_CREATE)
|
2021-04-23 02:59:54 +08:00
|
|
|
}
|
|
|
|
|
2019-09-04 10:25:38 +08:00
|
|
|
const {
|
2019-09-04 23:36:27 +08:00
|
|
|
// state
|
2019-09-04 10:25:38 +08:00
|
|
|
data: dataOptions,
|
|
|
|
computed: computedOptions,
|
|
|
|
methods,
|
|
|
|
watch: watchOptions,
|
|
|
|
provide: provideOptions,
|
|
|
|
inject: injectOptions,
|
2019-09-04 23:36:27 +08:00
|
|
|
// lifecycle
|
2021-06-03 02:37:27 +08:00
|
|
|
created,
|
2019-09-04 10:25:38 +08:00
|
|
|
beforeMount,
|
|
|
|
mounted,
|
|
|
|
beforeUpdate,
|
|
|
|
updated,
|
2019-10-30 10:28:38 +08:00
|
|
|
activated,
|
|
|
|
deactivated,
|
2020-09-15 09:47:58 +08:00
|
|
|
beforeDestroy,
|
2019-09-05 22:07:43 +08:00
|
|
|
beforeUnmount,
|
2020-09-15 09:47:58 +08:00
|
|
|
destroyed,
|
2019-09-05 22:07:43 +08:00
|
|
|
unmounted,
|
2020-07-28 05:44:17 +08:00
|
|
|
render,
|
2019-09-04 10:25:38 +08:00
|
|
|
renderTracked,
|
|
|
|
renderTriggered,
|
2020-11-15 01:49:35 +08:00
|
|
|
errorCaptured,
|
2021-05-08 00:00:52 +08:00
|
|
|
serverPrefetch,
|
2020-11-15 01:49:35 +08:00
|
|
|
// public API
|
2021-05-28 09:53:41 +08:00
|
|
|
expose,
|
2021-06-03 02:37:27 +08:00
|
|
|
inheritAttrs,
|
|
|
|
// assets
|
|
|
|
components,
|
|
|
|
directives,
|
|
|
|
filters
|
2019-09-04 23:36:27 +08:00
|
|
|
} = options
|
|
|
|
|
2020-04-06 06:39:22 +08:00
|
|
|
const checkDuplicateProperties = __DEV__ ? createDuplicateChecker() : null
|
|
|
|
|
2020-06-09 23:27:40 +08:00
|
|
|
if (__DEV__) {
|
2020-09-01 06:32:07 +08:00
|
|
|
const [propsOptions] = instance.propsOptions
|
2020-06-09 23:27:40 +08:00
|
|
|
if (propsOptions) {
|
|
|
|
for (const key in propsOptions) {
|
|
|
|
checkDuplicateProperties!(OptionTypes.PROPS, key)
|
|
|
|
}
|
2019-10-22 11:47:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 04:36:18 +08:00
|
|
|
// options initialization order (to be consistent with Vue 2):
|
|
|
|
// - props (already done outside of this function)
|
|
|
|
// - inject
|
|
|
|
// - methods
|
|
|
|
// - data (deferred since it relies on `this` access)
|
|
|
|
// - computed
|
|
|
|
// - watch (deferred since it relies on `this` access)
|
|
|
|
|
|
|
|
if (injectOptions) {
|
2021-07-28 05:52:31 +08:00
|
|
|
resolveInjections(
|
|
|
|
injectOptions,
|
|
|
|
ctx,
|
|
|
|
checkDuplicateProperties,
|
|
|
|
instance.appContext.config.unwrapInjectedRef
|
|
|
|
)
|
2020-04-23 04:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (methods) {
|
|
|
|
for (const key in methods) {
|
|
|
|
const methodHandler = (methods as MethodOptions)[key]
|
|
|
|
if (isFunction(methodHandler)) {
|
2021-07-28 05:52:31 +08:00
|
|
|
// In dev mode, we use the `createRenderContext` function to define
|
|
|
|
// methods to the proxy target, and those are read-only but
|
|
|
|
// reconfigurable, so it needs to be redefined here
|
2021-02-06 02:52:40 +08:00
|
|
|
if (__DEV__) {
|
|
|
|
Object.defineProperty(ctx, key, {
|
|
|
|
value: methodHandler.bind(publicThis),
|
|
|
|
configurable: true,
|
2021-02-25 22:04:28 +08:00
|
|
|
enumerable: true,
|
|
|
|
writable: true
|
2021-02-06 02:52:40 +08:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
ctx[key] = methodHandler.bind(publicThis)
|
|
|
|
}
|
2020-04-23 04:36:18 +08:00
|
|
|
if (__DEV__) {
|
|
|
|
checkDuplicateProperties!(OptionTypes.METHODS, key)
|
|
|
|
}
|
|
|
|
} else if (__DEV__) {
|
|
|
|
warn(
|
|
|
|
`Method "${key}" has type "${typeof methodHandler}" in the component definition. ` +
|
|
|
|
`Did you reference the function correctly?`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 02:37:27 +08:00
|
|
|
if (dataOptions) {
|
|
|
|
if (__DEV__ && !isFunction(dataOptions)) {
|
|
|
|
warn(
|
|
|
|
`The data option must be a function. ` +
|
|
|
|
`Plain object usage is no longer supported.`
|
|
|
|
)
|
2020-04-23 04:36:18 +08:00
|
|
|
}
|
2021-08-17 06:19:44 +08:00
|
|
|
const data = dataOptions.call(publicThis, publicThis)
|
2021-06-03 02:37:27 +08:00
|
|
|
if (__DEV__ && isPromise(data)) {
|
|
|
|
warn(
|
|
|
|
`data() returned a Promise - note data() cannot be async; If you ` +
|
|
|
|
`intend to perform data fetching before component renders, use ` +
|
|
|
|
`async setup() + <Suspense>.`
|
|
|
|
)
|
2020-08-25 09:30:04 +08:00
|
|
|
}
|
2021-06-03 02:37:27 +08:00
|
|
|
if (!isObject(data)) {
|
|
|
|
__DEV__ && warn(`data() should return an object.`)
|
|
|
|
} else {
|
|
|
|
instance.data = reactive(data)
|
|
|
|
if (__DEV__) {
|
|
|
|
for (const key in data) {
|
|
|
|
checkDuplicateProperties!(OptionTypes.DATA, key)
|
|
|
|
// expose data on ctx during dev
|
2022-05-13 16:52:19 +08:00
|
|
|
if (!isReservedPrefix(key[0])) {
|
2021-06-03 02:37:27 +08:00
|
|
|
Object.defineProperty(ctx, key, {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: () => data[key],
|
|
|
|
set: NOOP
|
|
|
|
})
|
|
|
|
}
|
2020-05-01 23:23:01 +08:00
|
|
|
}
|
2019-10-22 11:47:16 +08:00
|
|
|
}
|
2019-10-02 22:03:43 +08:00
|
|
|
}
|
2019-09-04 10:25:38 +08:00
|
|
|
}
|
2020-01-28 04:15:22 +08:00
|
|
|
|
2021-06-03 02:37:27 +08:00
|
|
|
// state initialization complete at this point - start caching access
|
|
|
|
shouldCacheAccess = true
|
|
|
|
|
2019-09-04 10:25:38 +08:00
|
|
|
if (computedOptions) {
|
|
|
|
for (const key in computedOptions) {
|
2019-09-06 04:09:30 +08:00
|
|
|
const opt = (computedOptions as ComputedOptions)[key]
|
2020-04-17 00:49:50 +08:00
|
|
|
const get = isFunction(opt)
|
|
|
|
? opt.bind(publicThis, publicThis)
|
|
|
|
: isFunction(opt.get)
|
2021-07-20 06:22:19 +08:00
|
|
|
? opt.get.bind(publicThis, publicThis)
|
|
|
|
: NOOP
|
2020-04-17 00:49:50 +08:00
|
|
|
if (__DEV__ && get === NOOP) {
|
|
|
|
warn(`Computed property "${key}" has no getter.`)
|
2019-10-14 14:15:31 +08:00
|
|
|
}
|
2020-04-17 00:49:50 +08:00
|
|
|
const set =
|
|
|
|
!isFunction(opt) && isFunction(opt.set)
|
|
|
|
? opt.set.bind(publicThis)
|
|
|
|
: __DEV__
|
2021-07-20 06:22:19 +08:00
|
|
|
? () => {
|
|
|
|
warn(
|
|
|
|
`Write operation failed: computed property "${key}" is readonly.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
: NOOP
|
2020-04-17 00:49:50 +08:00
|
|
|
const c = computed({
|
|
|
|
get,
|
|
|
|
set
|
|
|
|
})
|
|
|
|
Object.defineProperty(ctx, key, {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
get: () => c.value,
|
|
|
|
set: v => (c.value = v)
|
|
|
|
})
|
2020-04-06 06:39:22 +08:00
|
|
|
if (__DEV__) {
|
|
|
|
checkDuplicateProperties!(OptionTypes.COMPUTED, key)
|
|
|
|
}
|
2019-09-04 10:25:38 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-22 11:47:16 +08:00
|
|
|
|
2019-09-04 10:25:38 +08:00
|
|
|
if (watchOptions) {
|
2021-06-03 02:37:27 +08:00
|
|
|
for (const key in watchOptions) {
|
|
|
|
createWatcher(watchOptions[key], ctx, publicThis, key)
|
|
|
|
}
|
2019-09-04 10:25:38 +08:00
|
|
|
}
|
2020-01-28 04:15:22 +08:00
|
|
|
|
2019-09-04 10:25:38 +08:00
|
|
|
if (provideOptions) {
|
2021-06-03 02:37:27 +08:00
|
|
|
const provides = isFunction(provideOptions)
|
|
|
|
? provideOptions.call(publicThis)
|
|
|
|
: provideOptions
|
|
|
|
Reflect.ownKeys(provides).forEach(key => {
|
|
|
|
provide(key, provides[key])
|
2020-10-08 09:47:01 +08:00
|
|
|
})
|
2019-09-04 10:25:38 +08:00
|
|
|
}
|
2020-01-28 04:15:22 +08:00
|
|
|
|
2021-06-03 02:37:27 +08:00
|
|
|
if (created) {
|
|
|
|
callHook(created, instance, LifecycleHooks.CREATED)
|
2019-09-04 10:25:38 +08:00
|
|
|
}
|
2021-05-13 05:13:44 +08:00
|
|
|
|
|
|
|
function registerLifecycleHook(
|
|
|
|
register: Function,
|
|
|
|
hook?: Function | Function[]
|
|
|
|
) {
|
2021-06-03 02:37:27 +08:00
|
|
|
if (isArray(hook)) {
|
2021-05-13 05:13:44 +08:00
|
|
|
hook.forEach(_hook => register(_hook.bind(publicThis)))
|
|
|
|
} else if (hook) {
|
|
|
|
register((hook as Function).bind(publicThis))
|
|
|
|
}
|
2021-05-08 00:00:52 +08:00
|
|
|
}
|
2020-11-15 01:49:35 +08:00
|
|
|
|
2021-05-13 05:13:44 +08:00
|
|
|
registerLifecycleHook(onBeforeMount, beforeMount)
|
|
|
|
registerLifecycleHook(onMounted, mounted)
|
|
|
|
registerLifecycleHook(onBeforeUpdate, beforeUpdate)
|
|
|
|
registerLifecycleHook(onUpdated, updated)
|
|
|
|
registerLifecycleHook(onActivated, activated)
|
|
|
|
registerLifecycleHook(onDeactivated, deactivated)
|
|
|
|
registerLifecycleHook(onErrorCaptured, errorCaptured)
|
|
|
|
registerLifecycleHook(onRenderTracked, renderTracked)
|
|
|
|
registerLifecycleHook(onRenderTriggered, renderTriggered)
|
|
|
|
registerLifecycleHook(onBeforeUnmount, beforeUnmount)
|
|
|
|
registerLifecycleHook(onUnmounted, unmounted)
|
|
|
|
registerLifecycleHook(onServerPrefetch, serverPrefetch)
|
|
|
|
|
2021-04-06 22:07:16 +08:00
|
|
|
if (__COMPAT__) {
|
2021-04-08 03:38:04 +08:00
|
|
|
if (
|
|
|
|
beforeDestroy &&
|
2021-04-10 06:52:14 +08:00
|
|
|
softAssertCompatEnabled(DeprecationTypes.OPTIONS_BEFORE_DESTROY, instance)
|
2021-04-08 03:38:04 +08:00
|
|
|
) {
|
2021-05-13 05:13:44 +08:00
|
|
|
registerLifecycleHook(onBeforeUnmount, beforeDestroy)
|
2021-04-06 22:07:16 +08:00
|
|
|
}
|
2021-04-08 03:38:04 +08:00
|
|
|
if (
|
|
|
|
destroyed &&
|
2021-04-10 06:52:14 +08:00
|
|
|
softAssertCompatEnabled(DeprecationTypes.OPTIONS_DESTROYED, instance)
|
2021-04-08 03:38:04 +08:00
|
|
|
) {
|
2021-05-13 05:13:44 +08:00
|
|
|
registerLifecycleHook(onUnmounted, destroyed)
|
2021-04-06 22:07:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 00:28:37 +08:00
|
|
|
if (isArray(expose)) {
|
2021-06-03 02:37:27 +08:00
|
|
|
if (expose.length) {
|
2021-06-25 09:28:09 +08:00
|
|
|
const exposed = instance.exposed || (instance.exposed = {})
|
2021-06-03 02:37:27 +08:00
|
|
|
expose.forEach(key => {
|
2021-06-25 09:28:09 +08:00
|
|
|
Object.defineProperty(exposed, key, {
|
|
|
|
get: () => publicThis[key],
|
|
|
|
set: val => (publicThis[key] = val)
|
|
|
|
})
|
2021-06-03 02:37:27 +08:00
|
|
|
})
|
|
|
|
} else if (!instance.exposed) {
|
2021-06-25 09:28:09 +08:00
|
|
|
instance.exposed = {}
|
2020-11-17 00:28:37 +08:00
|
|
|
}
|
2020-11-15 01:49:35 +08:00
|
|
|
}
|
2021-05-28 09:53:41 +08:00
|
|
|
|
|
|
|
// options that are handled when creating the instance but also need to be
|
|
|
|
// applied from mixins
|
2021-06-03 02:37:27 +08:00
|
|
|
if (render && instance.render === NOOP) {
|
|
|
|
instance.render = render as InternalRenderFunction
|
|
|
|
}
|
|
|
|
if (inheritAttrs != null) {
|
|
|
|
instance.inheritAttrs = inheritAttrs
|
2021-05-28 09:53:41 +08:00
|
|
|
}
|
2019-09-05 22:07:43 +08:00
|
|
|
|
2021-06-03 02:37:27 +08:00
|
|
|
// asset options.
|
|
|
|
if (components) instance.components = components as any
|
|
|
|
if (directives) instance.directives = directives
|
|
|
|
if (
|
|
|
|
__COMPAT__ &&
|
|
|
|
filters &&
|
|
|
|
isCompatEnabled(DeprecationTypes.FILTERS, instance)
|
|
|
|
) {
|
|
|
|
instance.filters = filters
|
2021-04-20 00:08:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 03:14:14 +08:00
|
|
|
export function resolveInjections(
|
|
|
|
injectOptions: ComponentInjectOptions,
|
|
|
|
ctx: any,
|
2021-07-28 05:52:31 +08:00
|
|
|
checkDuplicateProperties = NOOP as any,
|
|
|
|
unwrapRef = false
|
2021-04-10 03:14:14 +08:00
|
|
|
) {
|
|
|
|
if (isArray(injectOptions)) {
|
2021-06-03 02:37:27 +08:00
|
|
|
injectOptions = normalizeInject(injectOptions)!
|
|
|
|
}
|
|
|
|
for (const key in injectOptions) {
|
|
|
|
const opt = (injectOptions as ObjectInjectOptions)[key]
|
2021-07-28 05:52:31 +08:00
|
|
|
let injected: unknown
|
2021-06-03 02:37:27 +08:00
|
|
|
if (isObject(opt)) {
|
|
|
|
if ('default' in opt) {
|
2021-07-28 05:52:31 +08:00
|
|
|
injected = inject(
|
2021-04-10 03:14:14 +08:00
|
|
|
opt.from || key,
|
|
|
|
opt.default,
|
|
|
|
true /* treat default function as factory */
|
|
|
|
)
|
|
|
|
} else {
|
2021-07-28 05:52:31 +08:00
|
|
|
injected = inject(opt.from || key)
|
2021-04-10 03:14:14 +08:00
|
|
|
}
|
2021-06-03 02:37:27 +08:00
|
|
|
} else {
|
2021-07-28 05:52:31 +08:00
|
|
|
injected = inject(opt)
|
|
|
|
}
|
|
|
|
if (isRef(injected)) {
|
|
|
|
// TODO remove the check in 3.3
|
|
|
|
if (unwrapRef) {
|
|
|
|
Object.defineProperty(ctx, key, {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
get: () => (injected as Ref).value,
|
|
|
|
set: v => ((injected as Ref).value = v)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if (__DEV__) {
|
|
|
|
warn(
|
|
|
|
`injected property "${key}" is a ref and will be auto-unwrapped ` +
|
|
|
|
`and no longer needs \`.value\` in the next minor release. ` +
|
|
|
|
`To opt-in to the new behavior now, ` +
|
|
|
|
`set \`app.config.unwrapInjectedRef = true\` (this config is ` +
|
|
|
|
`temporary and will not be needed in the future.)`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ctx[key] = injected
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx[key] = injected
|
2021-04-10 03:14:14 +08:00
|
|
|
}
|
2021-06-03 02:37:27 +08:00
|
|
|
if (__DEV__) {
|
|
|
|
checkDuplicateProperties!(OptionTypes.INJECT, key)
|
2019-09-05 22:07:43 +08:00
|
|
|
}
|
2019-09-04 10:25:38 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-04 11:04:11 +08:00
|
|
|
|
2021-06-03 02:37:27 +08:00
|
|
|
function callHook(
|
|
|
|
hook: Function,
|
2019-09-07 00:58:31 +08:00
|
|
|
instance: ComponentInternalInstance,
|
2021-06-03 02:37:27 +08:00
|
|
|
type: LifecycleHooks
|
2019-09-07 00:58:31 +08:00
|
|
|
) {
|
2021-06-03 02:37:27 +08:00
|
|
|
callWithAsyncErrorHandling(
|
|
|
|
isArray(hook)
|
|
|
|
? hook.map(h => h.bind(instance.proxy!))
|
|
|
|
: hook.bind(instance.proxy!),
|
|
|
|
instance,
|
|
|
|
type
|
|
|
|
)
|
2019-09-04 23:36:27 +08:00
|
|
|
}
|
2019-10-25 22:25:52 +08:00
|
|
|
|
2021-04-22 21:49:25 +08:00
|
|
|
export function createWatcher(
|
2019-10-25 22:25:52 +08:00
|
|
|
raw: ComponentWatchOptionItem,
|
2020-04-17 00:49:50 +08:00
|
|
|
ctx: Data,
|
|
|
|
publicThis: ComponentPublicInstance,
|
2019-10-25 22:25:52 +08:00
|
|
|
key: string
|
|
|
|
) {
|
2020-09-18 12:00:39 +08:00
|
|
|
const getter = key.includes('.')
|
|
|
|
? createPathGetter(publicThis, key)
|
|
|
|
: () => (publicThis as any)[key]
|
2019-10-25 22:25:52 +08:00
|
|
|
if (isString(raw)) {
|
2020-04-17 00:49:50 +08:00
|
|
|
const handler = ctx[raw]
|
2019-10-25 22:25:52 +08:00
|
|
|
if (isFunction(handler)) {
|
2019-12-31 00:30:12 +08:00
|
|
|
watch(getter, handler as WatchCallback)
|
2019-10-25 22:25:52 +08:00
|
|
|
} else if (__DEV__) {
|
|
|
|
warn(`Invalid watch handler specified by key "${raw}"`, handler)
|
|
|
|
}
|
|
|
|
} else if (isFunction(raw)) {
|
2020-04-17 00:49:50 +08:00
|
|
|
watch(getter, raw.bind(publicThis))
|
2019-10-25 22:25:52 +08:00
|
|
|
} else if (isObject(raw)) {
|
|
|
|
if (isArray(raw)) {
|
2020-04-17 00:49:50 +08:00
|
|
|
raw.forEach(r => createWatcher(r, ctx, publicThis, key))
|
2019-10-25 22:25:52 +08:00
|
|
|
} else {
|
2020-08-05 00:42:47 +08:00
|
|
|
const handler = isFunction(raw.handler)
|
|
|
|
? raw.handler.bind(publicThis)
|
|
|
|
: (ctx[raw.handler] as WatchCallback)
|
|
|
|
if (isFunction(handler)) {
|
|
|
|
watch(getter, handler, raw)
|
|
|
|
} else if (__DEV__) {
|
|
|
|
warn(`Invalid watch handler specified by key "${raw.handler}"`, handler)
|
|
|
|
}
|
2019-10-25 22:25:52 +08:00
|
|
|
}
|
|
|
|
} else if (__DEV__) {
|
2020-09-18 12:00:39 +08:00
|
|
|
warn(`Invalid watch option: "${key}"`, raw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 22:37:50 +08:00
|
|
|
/**
|
|
|
|
* Resolve merged options and cache it on the component.
|
|
|
|
* This is done only once per-component since the merging does not involve
|
|
|
|
* instances.
|
|
|
|
*/
|
2020-03-24 23:59:00 +08:00
|
|
|
export function resolveMergedOptions(
|
|
|
|
instance: ComponentInternalInstance
|
2021-06-03 02:37:27 +08:00
|
|
|
): MergedComponentOptions {
|
2021-06-02 22:37:50 +08:00
|
|
|
const base = instance.type as ComponentOptions
|
|
|
|
const { mixins, extends: extendsOptions } = base
|
|
|
|
const {
|
|
|
|
mixins: globalMixins,
|
2021-06-03 03:22:52 +08:00
|
|
|
optionsCache: cache,
|
2021-06-02 22:37:50 +08:00
|
|
|
config: { optionMergeStrategies }
|
|
|
|
} = instance.appContext
|
|
|
|
const cached = cache.get(base)
|
|
|
|
|
2021-06-03 02:37:27 +08:00
|
|
|
let resolved: MergedComponentOptions
|
2021-06-02 22:37:50 +08:00
|
|
|
|
|
|
|
if (cached) {
|
|
|
|
resolved = cached
|
|
|
|
} else if (!globalMixins.length && !mixins && !extendsOptions) {
|
|
|
|
if (
|
|
|
|
__COMPAT__ &&
|
|
|
|
isCompatEnabled(DeprecationTypes.PRIVATE_APIS, instance)
|
|
|
|
) {
|
2021-06-03 02:37:27 +08:00
|
|
|
resolved = extend({}, base) as MergedComponentOptions
|
2021-06-02 22:37:50 +08:00
|
|
|
resolved.parent = instance.parent && instance.parent.proxy
|
|
|
|
resolved.propsData = instance.vnode.props
|
|
|
|
} else {
|
2021-06-03 02:37:27 +08:00
|
|
|
resolved = base as MergedComponentOptions
|
2021-06-02 22:37:50 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resolved = {}
|
|
|
|
if (globalMixins.length) {
|
|
|
|
globalMixins.forEach(m =>
|
2021-06-03 02:37:27 +08:00
|
|
|
mergeOptions(resolved, m, optionMergeStrategies, true)
|
2021-06-02 22:37:50 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
mergeOptions(resolved, base, optionMergeStrategies)
|
|
|
|
}
|
|
|
|
|
|
|
|
cache.set(base, resolved)
|
|
|
|
return resolved
|
2020-03-24 23:59:00 +08:00
|
|
|
}
|
|
|
|
|
2021-04-12 04:53:43 +08:00
|
|
|
export function mergeOptions(
|
|
|
|
to: any,
|
|
|
|
from: any,
|
2021-06-03 02:37:27 +08:00
|
|
|
strats: Record<string, OptionMergeFunction>,
|
|
|
|
asMixin = false
|
2021-04-12 04:53:43 +08:00
|
|
|
) {
|
2021-04-23 02:59:54 +08:00
|
|
|
if (__COMPAT__ && isFunction(from)) {
|
|
|
|
from = from.options
|
|
|
|
}
|
|
|
|
|
2020-09-03 22:35:43 +08:00
|
|
|
const { mixins, extends: extendsOptions } = from
|
|
|
|
|
2021-06-02 22:37:50 +08:00
|
|
|
if (extendsOptions) {
|
2021-06-03 02:37:27 +08:00
|
|
|
mergeOptions(to, extendsOptions, strats, true)
|
2021-06-02 22:37:50 +08:00
|
|
|
}
|
|
|
|
if (mixins) {
|
2021-06-03 02:37:27 +08:00
|
|
|
mixins.forEach((m: ComponentOptionsMixin) =>
|
|
|
|
mergeOptions(to, m, strats, true)
|
|
|
|
)
|
2021-06-02 22:37:50 +08:00
|
|
|
}
|
2020-09-03 22:35:43 +08:00
|
|
|
|
2020-03-24 23:59:00 +08:00
|
|
|
for (const key in from) {
|
2021-06-03 02:37:27 +08:00
|
|
|
if (asMixin && key === 'expose') {
|
|
|
|
__DEV__ &&
|
|
|
|
warn(
|
|
|
|
`"expose" option is ignored when declared in mixins or extends. ` +
|
|
|
|
`It should only be declared in the base component itself.`
|
|
|
|
)
|
2020-09-03 22:35:43 +08:00
|
|
|
} else {
|
2021-06-03 02:37:27 +08:00
|
|
|
const strat = internalOptionMergeStrats[key] || (strats && strats[key])
|
|
|
|
to[key] = strat ? strat(to[key], from[key]) : from[key]
|
2020-03-24 23:59:00 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-23 02:59:54 +08:00
|
|
|
return to
|
2020-03-24 23:59:00 +08:00
|
|
|
}
|
2021-06-03 02:37:27 +08:00
|
|
|
|
|
|
|
export const internalOptionMergeStrats: Record<string, Function> = {
|
|
|
|
data: mergeDataFn,
|
|
|
|
props: mergeObjectOptions, // TODO
|
|
|
|
emits: mergeObjectOptions, // TODO
|
|
|
|
// objects
|
|
|
|
methods: mergeObjectOptions,
|
|
|
|
computed: mergeObjectOptions,
|
|
|
|
// lifecycle
|
2021-06-23 01:54:43 +08:00
|
|
|
beforeCreate: mergeAsArray,
|
|
|
|
created: mergeAsArray,
|
|
|
|
beforeMount: mergeAsArray,
|
|
|
|
mounted: mergeAsArray,
|
|
|
|
beforeUpdate: mergeAsArray,
|
|
|
|
updated: mergeAsArray,
|
|
|
|
beforeDestroy: mergeAsArray,
|
2021-09-02 04:41:57 +08:00
|
|
|
beforeUnmount: mergeAsArray,
|
2021-06-23 01:54:43 +08:00
|
|
|
destroyed: mergeAsArray,
|
2021-09-02 04:41:57 +08:00
|
|
|
unmounted: mergeAsArray,
|
2021-06-23 01:54:43 +08:00
|
|
|
activated: mergeAsArray,
|
|
|
|
deactivated: mergeAsArray,
|
|
|
|
errorCaptured: mergeAsArray,
|
|
|
|
serverPrefetch: mergeAsArray,
|
2021-06-03 02:37:27 +08:00
|
|
|
// assets
|
|
|
|
components: mergeObjectOptions,
|
|
|
|
directives: mergeObjectOptions,
|
2021-06-23 01:54:43 +08:00
|
|
|
// watch
|
|
|
|
watch: mergeWatchOptions,
|
2021-06-03 02:37:27 +08:00
|
|
|
// provide / inject
|
|
|
|
provide: mergeDataFn,
|
|
|
|
inject: mergeInject
|
|
|
|
}
|
|
|
|
|
|
|
|
if (__COMPAT__) {
|
|
|
|
internalOptionMergeStrats.filters = mergeObjectOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
function mergeDataFn(to: any, from: any) {
|
|
|
|
if (!from) {
|
|
|
|
return to
|
|
|
|
}
|
|
|
|
if (!to) {
|
|
|
|
return from
|
|
|
|
}
|
|
|
|
return function mergedDataFn(this: ComponentPublicInstance) {
|
2021-07-20 06:22:19 +08:00
|
|
|
return (
|
|
|
|
__COMPAT__ && isCompatEnabled(DeprecationTypes.OPTIONS_DATA_MERGE, null)
|
|
|
|
? deepMergeData
|
|
|
|
: extend
|
|
|
|
)(
|
2021-06-03 02:37:27 +08:00
|
|
|
isFunction(to) ? to.call(this, this) : to,
|
|
|
|
isFunction(from) ? from.call(this, this) : from
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mergeInject(
|
|
|
|
to: ComponentInjectOptions | undefined,
|
|
|
|
from: ComponentInjectOptions
|
|
|
|
) {
|
|
|
|
return mergeObjectOptions(normalizeInject(to), normalizeInject(from))
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeInject(
|
|
|
|
raw: ComponentInjectOptions | undefined
|
|
|
|
): ObjectInjectOptions | undefined {
|
|
|
|
if (isArray(raw)) {
|
|
|
|
const res: ObjectInjectOptions = {}
|
|
|
|
for (let i = 0; i < raw.length; i++) {
|
|
|
|
res[raw[i]] = raw[i]
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
return raw
|
|
|
|
}
|
|
|
|
|
2021-06-23 01:54:43 +08:00
|
|
|
function mergeAsArray<T = Function>(to: T[] | T | undefined, from: T | T[]) {
|
2021-06-03 02:37:27 +08:00
|
|
|
return to ? [...new Set([].concat(to as any, from as any))] : from
|
|
|
|
}
|
|
|
|
|
|
|
|
function mergeObjectOptions(to: Object | undefined, from: Object | undefined) {
|
|
|
|
return to ? extend(extend(Object.create(null), to), from) : from
|
|
|
|
}
|
2021-06-23 01:54:43 +08:00
|
|
|
|
|
|
|
function mergeWatchOptions(
|
|
|
|
to: ComponentWatchOptions | undefined,
|
|
|
|
from: ComponentWatchOptions | undefined
|
|
|
|
) {
|
|
|
|
if (!to) return from
|
|
|
|
if (!from) return to
|
|
|
|
const merged = extend(Object.create(null), to)
|
|
|
|
for (const key in from) {
|
|
|
|
merged[key] = mergeAsArray(to[key], from[key])
|
|
|
|
}
|
|
|
|
return merged
|
|
|
|
}
|