Ver Fonte

auto default content insertion when no template option is provided

Evan You há 11 anos atrás
pai
commit
942e2bb56f
2 ficheiros alterados com 17 adições e 4 exclusões
  1. 9 4
      src/compiler/transclude.js
  2. 8 0
      test/unit/specs/compiler/content_spec.js

+ 9 - 4
src/compiler/transclude.js

@@ -28,8 +28,14 @@ exports.transclude = function (el, options) {
   if (_.isTemplate(el)) {
     el = templateParser.parse(el)
   }
-  if (options && options.template) {
-    el = transcludeTemplate(el, options)
+  if (options) {
+    if (options._asComponent && !options.template) {
+      options.template = '<content></content>'
+    }
+    if (options.template) {
+      options._content = _.extractContent(el)
+      el = transcludeTemplate(el, options)
+    }
   }
   if (el instanceof DocumentFragment) {
     // anchors for block instance
@@ -56,7 +62,6 @@ function transcludeTemplate (el, options) {
   if (!frag) {
     _.warn('Invalid template option: ' + template)
   } else {
-    options._content = _.extractContent(el)
     var replacer = frag.firstChild
     if (options.replace) {
       if (
@@ -121,4 +126,4 @@ function mergeAttrs (from, to) {
       to.className = to.className + ' ' + value
     }
   }
-}
+}

+ 8 - 0
test/unit/specs/compiler/content_spec.js

@@ -30,6 +30,14 @@ describe('Content Transclusion', function () {
     expect(el.firstChild.firstChild.textContent).toBe('hi')
   })
 
+  it('no template auto content', function () {
+    el.innerHTML = '<p>hi</p>'
+    options._asComponent = true
+    mount()
+    expect(el.firstChild.tagName).toBe('P')
+    expect(el.firstChild.textContent).toBe('hi')
+  })
+
   it('fallback content', function () {
     options.template = '<content><p>fallback</p></content>'
     mount()