apiOptions.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import {
  2. ComponentInstance,
  3. Data,
  4. ComponentOptions,
  5. currentRenderingInstance,
  6. currentInstance
  7. } from './component'
  8. import {
  9. isFunction,
  10. extend,
  11. isString,
  12. isObject,
  13. isArray,
  14. EMPTY_OBJ,
  15. capitalize,
  16. camelize
  17. } from '@vue/shared'
  18. import { computed, ComputedOptions } from './apiReactivity'
  19. import { watch } from './apiWatch'
  20. import { provide, inject } from './apiInject'
  21. import {
  22. onBeforeMount,
  23. onMounted,
  24. onBeforeUpdate,
  25. onUpdated,
  26. onErrorCaptured,
  27. onRenderTracked,
  28. onBeforeUnmount,
  29. onUnmounted
  30. } from './apiLifecycle'
  31. import { DebuggerEvent } from '@vue/reactivity'
  32. import { warn } from './warning'
  33. // TODO legacy component definition also supports constructors with .options
  34. type LegacyComponent = ComponentOptions
  35. // TODO type inference for these options
  36. export interface LegacyOptions {
  37. el?: any
  38. // state
  39. data?: Data | (() => Data)
  40. computed?: Record<string, (() => any) | ComputedOptions>
  41. methods?: Record<string, Function>
  42. // TODO watch array
  43. watch?: Record<
  44. string,
  45. | string
  46. | Function
  47. | { handler: Function; deep?: boolean; immediate: boolean }
  48. >
  49. provide?: Data | (() => Data)
  50. inject?:
  51. | string[]
  52. | Record<
  53. string | symbol,
  54. string | symbol | { from: string | symbol; default: any }
  55. >
  56. // composition
  57. mixins?: LegacyComponent[]
  58. extends?: LegacyComponent
  59. // lifecycle
  60. beforeCreate?(): void
  61. created?(): void
  62. beforeMount?(): void
  63. mounted?(): void
  64. beforeUpdate?(): void
  65. updated?(): void
  66. activated?(): void
  67. decativated?(): void
  68. beforeDestroy?(): void
  69. destroyed?(): void
  70. renderTracked?(e: DebuggerEvent): void
  71. renderTriggered?(e: DebuggerEvent): void
  72. errorCaptured?(): boolean
  73. }
  74. export function applyOptions(
  75. instance: ComponentInstance,
  76. options: ComponentOptions,
  77. asMixin: boolean = false
  78. ) {
  79. const data =
  80. instance.data === EMPTY_OBJ ? (instance.data = {}) : instance.data
  81. const ctx = instance.renderProxy as any
  82. const {
  83. // composition
  84. mixins,
  85. extends: extendsOptions,
  86. // state
  87. data: dataOptions,
  88. computed: computedOptions,
  89. methods,
  90. watch: watchOptions,
  91. provide: provideOptions,
  92. inject: injectOptions,
  93. // assets
  94. components,
  95. directives,
  96. // lifecycle
  97. // beforeCreate is handled separately
  98. created,
  99. beforeMount,
  100. mounted,
  101. beforeUpdate,
  102. updated,
  103. // TODO activated
  104. // TODO decativated
  105. beforeDestroy,
  106. destroyed,
  107. renderTracked,
  108. renderTriggered,
  109. errorCaptured
  110. } = options
  111. // global mixins are applied first, and only if this is a non-mixin call
  112. // so that they are applied once per instance.
  113. if (!asMixin) {
  114. applyMixins(instance, instance.appContext.mixins)
  115. }
  116. // extending a base component...
  117. if (extendsOptions) {
  118. applyOptions(instance, extendsOptions, true)
  119. }
  120. // local mixins
  121. if (mixins) {
  122. applyMixins(instance, mixins)
  123. }
  124. // state options
  125. if (dataOptions) {
  126. extend(data, isFunction(dataOptions) ? dataOptions.call(ctx) : dataOptions)
  127. }
  128. if (computedOptions) {
  129. for (const key in computedOptions) {
  130. data[key] = computed(computedOptions[key] as any)
  131. }
  132. }
  133. if (methods) {
  134. for (const key in methods) {
  135. data[key] = methods[key].bind(ctx)
  136. }
  137. }
  138. if (watchOptions) {
  139. for (const key in watchOptions) {
  140. const raw = watchOptions[key]
  141. const getter = () => ctx[key]
  142. if (isString(raw)) {
  143. const handler = data[key]
  144. if (isFunction(handler)) {
  145. watch(getter, handler.bind(ctx))
  146. } else if (__DEV__) {
  147. // TODO warn invalid watch handler path
  148. }
  149. } else if (isFunction(raw)) {
  150. watch(getter, raw.bind(ctx))
  151. } else if (isObject(raw)) {
  152. // TODO 2.x compat
  153. watch(getter, raw.handler.bind(ctx), raw)
  154. } else if (__DEV__) {
  155. // TODO warn invalid watch options
  156. }
  157. }
  158. }
  159. if (provideOptions) {
  160. const provides = isFunction(provideOptions)
  161. ? provideOptions.call(ctx)
  162. : provideOptions
  163. for (const key in provides) {
  164. provide(key, provides[key])
  165. }
  166. }
  167. if (injectOptions) {
  168. if (isArray(injectOptions)) {
  169. for (let i = 0; i < injectOptions.length; i++) {
  170. const key = injectOptions[i]
  171. data[key] = inject(key)
  172. }
  173. } else {
  174. for (const key in injectOptions) {
  175. const opt = injectOptions[key]
  176. if (isObject(opt)) {
  177. data[key] = inject(opt.from, opt.default)
  178. } else {
  179. data[key] = inject(opt)
  180. }
  181. }
  182. }
  183. }
  184. // asset options
  185. if (components) {
  186. extend(instance.components, components)
  187. }
  188. if (directives) {
  189. extend(instance.directives, directives)
  190. }
  191. // lifecycle options
  192. if (created) {
  193. created.call(ctx)
  194. }
  195. if (beforeMount) {
  196. onBeforeMount(beforeMount.bind(ctx))
  197. }
  198. if (mounted) {
  199. onMounted(mounted.bind(ctx))
  200. }
  201. if (beforeUpdate) {
  202. onBeforeUpdate(beforeUpdate.bind(ctx))
  203. }
  204. if (updated) {
  205. onUpdated(updated.bind(ctx))
  206. }
  207. if (errorCaptured) {
  208. onErrorCaptured(errorCaptured.bind(ctx))
  209. }
  210. if (renderTracked) {
  211. onRenderTracked(renderTracked.bind(ctx))
  212. }
  213. if (renderTriggered) {
  214. onRenderTracked(renderTriggered.bind(ctx))
  215. }
  216. if (beforeDestroy) {
  217. onBeforeUnmount(beforeDestroy.bind(ctx))
  218. }
  219. if (destroyed) {
  220. onUnmounted(destroyed.bind(ctx))
  221. }
  222. }
  223. function applyMixins(instance: ComponentInstance, mixins: ComponentOptions[]) {
  224. for (let i = 0; i < mixins.length; i++) {
  225. applyOptions(instance, mixins[i], true)
  226. }
  227. }
  228. export function resolveAsset(type: 'components' | 'directives', name: string) {
  229. const instance = currentRenderingInstance || currentInstance
  230. if (instance) {
  231. let camelized
  232. const registry = instance[type]
  233. const res =
  234. registry[name] ||
  235. registry[(camelized = camelize(name))] ||
  236. registry[capitalize(camelized)]
  237. if (__DEV__ && !res) {
  238. warn(`Failed to resolve ${type.slice(0, -1)}: ${name}`)
  239. }
  240. return res
  241. } else if (__DEV__) {
  242. warn(
  243. `resolve${capitalize(type.slice(0, -1))} ` +
  244. `can only be used in render() or setup().`
  245. )
  246. }
  247. }