Browse Source

inherit child reference from old node when patching static nodes (fix #4288)

Evan You 9 years ago
parent
commit
1a7b910feb
2 changed files with 23 additions and 0 deletions
  1. 1 0
      src/core/vdom/patch.js
  2. 22 0
      test/unit/features/directives/once.spec.js

+ 1 - 0
src/core/vdom/patch.js

@@ -342,6 +342,7 @@ export function createPatchFunction (backend) {
         vnode.key === oldVnode.key &&
         (vnode.isCloned || vnode.isOnce)) {
       vnode.elm = oldVnode.elm
+      vnode.child = oldVnode.child
       return
     }
     let i

+ 22 - 0
test/unit/features/directives/once.spec.js

@@ -313,6 +313,28 @@ describe('Directive v-once', () => {
     expect(vm.$el.textContent).toBe('aabbcc')
     expect(`v-once can only be used inside v-for that is keyed.`).toHaveBeenWarned()
   })
+
+  // #4288
+  it('should inherit child reference for v-once', done => {
+    const vm = new Vue({
+      template: `<div>{{a}}<test v-if="ok" v-once></test></div>`,
+      data: {
+        a: 0,
+        ok: true
+      },
+      components: {
+        test: {
+          template: '<div>foo</div>'
+        }
+      }
+    }).$mount()
+    vm.a++ // first update to force a patch
+    waitForUpdate(() => {
+      expect(vm.$el.textContent).toBe('1foo')
+    }).then(() => {
+      vm.ok = false // teardown component with v-once
+    }).then(done) // should not throw
+  })
 })
 
 function expectTextContent (vm, text) {