Răsfoiți Sursa

support template slot

Evan You 10 ani în urmă
părinte
comite
1a35cf4a21

+ 1 - 2
src/compiler/codegen/index.js

@@ -3,7 +3,6 @@ import { genDirectives } from './directives/index'
 
 export function generate (ast) {
   const code = ast ? genElement(ast) : '__h__("div")'
-  console.log(code)
   return `with (this) { return ${code}}`
 }
 
@@ -12,7 +11,7 @@ function genElement (el) {
     return genFor(el)
   } else if (el.if) {
     return genIf(el)
-  } else if (el.tag === 'template') {
+  } else if (el.tag === 'template' && !el.attrsMap.slot) {
     return genChildren(el)
   } else if (el.tag === 'render') {
     return genRender(el)

+ 2 - 2
src/compiler/parser/index.js

@@ -82,7 +82,7 @@ export function parse (template, preserveWhitespace) {
           'Component template should contain exactly one root element:\n\n' + template
         )
       }
-      if (currentParent) {
+      if (currentParent && tag !== 'script') {
         currentParent.children.push(element)
       }
       if (!unary) {
@@ -91,7 +91,7 @@ export function parse (template, preserveWhitespace) {
       }
     },
 
-    end () {
+    end (tag) {
       // remove trailing whitespace
       const element = stack[stack.length - 1]
       const lastNode = element.children[element.children.length - 1]

+ 7 - 2
src/runtime/instance/render.js

@@ -19,13 +19,19 @@ export function initRender (vm) {
 
 function resolveSlots (vm, children) {
   if (children) {
+    children = children.slice()
     const slots = { default: children }
     let i = children.length
     let name, child
     while (i--) {
       child = children[i]
       if ((name = child.data && child.data.slot)) {
-        (slots[name] || (slots[name] = [])).push(child)
+        let slot = (slots[name] || (slots[name] = []))
+        if (child.tag === 'template') {
+          slot.push.apply(slot, child.children)
+        } else {
+          slot.push(child)
+        }
         children.splice(i, 1)
       }
     }
@@ -123,7 +129,6 @@ export function renderMixin (Vue) {
       mergeParentDirectives(this, data, _renderData)
       updateParentCallbacks(this, data, _renderData)
     }
-    console.log(vnode)
     return vnode
   }
 

+ 1 - 1
src/runtime/vdom/component.js

@@ -7,7 +7,7 @@ export default function Component (Ctor, data, parent, children) {
   // return a placeholder vnode
   const key = data && data.key
   return {
-    sel: 'component',
+    tag: 'component',
     key,
     data: {
       hook: { init, prepatch, destroy },

+ 4 - 4
src/runtime/vdom/patch.js

@@ -15,7 +15,7 @@ function isDef (s) {
 }
 
 function sameVnode (vnode1, vnode2) {
-  return vnode1.key === vnode2.key && vnode1.sel === vnode2.sel
+  return vnode1.key === vnode2.key && vnode1.tag === vnode2.tag
 }
 
 function getElm (vnode) {
@@ -68,7 +68,7 @@ export default function createPatchFunction (modules, api) {
       }
     }
     const children = vnode.children
-    const tag = vnode.sel
+    const tag = vnode.tag
     if (isDef(tag)) {
       elm = vnode.elm = isDef(data) && data.svg
         ? api.createElementNS(svgNS, tag)
@@ -118,7 +118,7 @@ export default function createPatchFunction (modules, api) {
       let i, listeners, rm
       const ch = vnodes[startIdx]
       if (isDef(ch)) {
-        if (isDef(ch.sel)) {
+        if (isDef(ch.tag)) {
           invokeDestroyHook(ch)
           listeners = cbs.remove.length + 1
           rm = createRmCb(getElm(ch), listeners)
@@ -245,7 +245,7 @@ export default function createPatchFunction (modules, api) {
     if (!oldVnode) {
       createElm(vnode, insertedVnodeQueue)
     } else {
-      if (isUndef(oldVnode.sel)) {
+      if (isUndef(oldVnode.tag)) {
         oldVnode = emptyNodeAt(oldVnode)
       }
 

+ 2 - 2
src/runtime/vdom/vnode.js

@@ -1,3 +1,3 @@
-export default function VNode (sel, data, children, text, elm) {
-  return { sel, data, children, text, elm, key: data && data.key }
+export default function VNode (tag, data, children, text, elm) {
+  return { tag, data, children, text, elm, key: data && data.key }
 }