Просмотр исходного кода

split create-functional-component

Evan You 9 лет назад
Родитель
Сommit
5818ac9203
2 измененных файлов с 47 добавлено и 43 удалено
  1. 3 43
      src/core/vdom/create-component.js
  2. 44 0
      src/core/vdom/create-functional-component.js

+ 3 - 43
src/core/vdom/create-component.js

@@ -1,18 +1,16 @@
 /* @flow */
 
 import VNode from './vnode'
-import { createElement } from './create-element'
-import { resolveConstructorOptions } from '../instance/init'
-import { resolveSlots } from '../instance/render-helpers/resolve-slots'
 import { resolveInject } from '../instance/inject'
+import { resolveConstructorOptions } from '../instance/init'
+import { createFunctionalComponent } from './create-functional-component'
 
 import {
   warn,
   isDef,
   isUndef,
   isTrue,
-  isObject,
-  validateProp
+  isObject
 } from '../util/index'
 
 import {
@@ -167,44 +165,6 @@ export function createComponent (
   return vnode
 }
 
-function createFunctionalComponent (
-  Ctor: Class<Component>,
-  propsData: ?Object,
-  data: VNodeData,
-  context: Component,
-  children: ?Array<VNode>
-): VNode | void {
-  const props = {}
-  const propOptions = Ctor.options.props
-  if (isDef(propOptions)) {
-    for (const key in propOptions) {
-      props[key] = validateProp(key, propOptions, propsData)
-    }
-  }
-  // ensure the createElement function in functional components
-  // gets a unique context - this is necessary for correct named slot check
-  const _context = Object.create(context)
-  const h = (a, b, c, d) => createElement(_context, a, b, c, d, true)
-
-  // functional injections should not reactive
-  const injections = resolveInject(Ctor.options.inject, _context)
-  const vnode = Ctor.options.render.call(null, h, {
-    props,
-    data,
-    parent: context,
-    children,
-    injections,
-    slots: () => resolveSlots(children, context)
-  })
-  if (vnode instanceof VNode) {
-    vnode.functionalContext = context
-    if (data.slot) {
-      (vnode.data || (vnode.data = {})).slot = data.slot
-    }
-  }
-  return vnode
-}
-
 export function createComponentInstanceForVnode (
   vnode: any, // we know it's MountedComponentVNode but flow doesn't
   parent: any, // activeInstance in lifecycle state

+ 44 - 0
src/core/vdom/create-functional-component.js

@@ -0,0 +1,44 @@
+/* @flow */
+
+import VNode from './vnode'
+import { createElement } from './create-element'
+import { resolveSlots } from '../instance/render-helpers/resolve-slots'
+
+import {
+  isDef,
+  validateProp
+} from '../util/index'
+
+export function createFunctionalComponent (
+  Ctor: Class<Component>,
+  propsData: ?Object,
+  data: VNodeData,
+  context: Component,
+  children: ?Array<VNode>
+): VNode | void {
+  const props = {}
+  const propOptions = Ctor.options.props
+  if (isDef(propOptions)) {
+    for (const key in propOptions) {
+      props[key] = validateProp(key, propOptions, propsData)
+    }
+  }
+  // ensure the createElement function in functional components
+  // gets a unique context - this is necessary for correct named slot check
+  const _context = Object.create(context)
+  const h = (a, b, c, d) => createElement(_context, a, b, c, d, true)
+  const vnode = Ctor.options.render.call(null, h, {
+    props,
+    data,
+    parent: context,
+    children,
+    slots: () => resolveSlots(children, context)
+  })
+  if (vnode instanceof VNode) {
+    vnode.functionalContext = context
+    if (data.slot) {
+      (vnode.data || (vnode.data = {})).slot = data.slot
+    }
+  }
+  return vnode
+}