2
0
Эх сурвалжийг харах

fallback for slot with v-if should be compiled in child scope (fix #2348)

Evan You 10 жил өмнө
parent
commit
9aef0f72ea

+ 2 - 0
src/directives/element/slot.js

@@ -73,6 +73,8 @@ export const slot = {
         const elseBlock = document.createElement('template')
         elseBlock.setAttribute('v-else', '')
         elseBlock.innerHTML = this.el.innerHTML
+        // the else block should be compiled in child scope
+        elseBlock._context = this.vm
         content.appendChild(elseBlock)
       }
       const scope = host

+ 1 - 1
src/directives/public/if.js

@@ -19,7 +19,7 @@ export default {
       var next = el.nextElementSibling
       if (next && getAttr(next, 'v-else') !== null) {
         remove(next)
-        this.elseFactory = new FragmentFactory(this.vm, next)
+        this.elseFactory = new FragmentFactory(next._context || this.vm, next)
       }
       // check main block
       this.anchor = createAnchor('v-if')

+ 7 - 3
test/unit/specs/directives/element/slot_spec.js

@@ -401,12 +401,16 @@ describe('Slot Distribution', function () {
     var vm = new Vue({
       el: el,
       data: {
-        ok: false
+        ok: false,
+        msg: 'inserted'
       },
-      template: '<div><comp><div v-if="ok">inserted</div></comp></div>',
+      template: '<div><comp><div v-if="ok">{{ msg }}</div></comp></div>',
       components: {
         comp: {
-          template: '<div><slot>fallback</slot></div>'
+          data: function () {
+            return { msg: 'fallback' }
+          },
+          template: '<div><slot>{{ msg }}</slot></div>'
         }
       }
     })