Browse Source

fix(reactivity): keep previous effect scope

三咲智子 Kevin Deng 2 years ago
parent
commit
8dec243dc1
2 changed files with 11 additions and 5 deletions
  1. 3 1
      packages/reactivity/src/effectScope.ts
  2. 8 4
      playground/src/v-for.js

+ 3 - 1
packages/reactivity/src/effectScope.ts

@@ -62,11 +62,13 @@ export class EffectScope {
     }
   }
 
+  prevScope: EffectScope | undefined
   /**
    * This should only be called on non-detached scopes
    * @internal
    */
   on() {
+    this.prevScope = activeEffectScope
     activeEffectScope = this
   }
 
@@ -75,7 +77,7 @@ export class EffectScope {
    * @internal
    */
   off() {
-    activeEffectScope = this.parent
+    activeEffectScope = this.prevScope
   }
 
   stop(fromParent?: boolean) {

+ 8 - 4
playground/src/v-for.js

@@ -23,12 +23,16 @@ export default defineComponent({
           const container = document.createElement('li')
           append(container, node)
 
-          const update = () => {
+          renderEffect(() => {
             const [item, index] = block.s
             node.textContent = `${index}. ${item}`
-          }
-          renderEffect(update)
-          return [container, update]
+          })
+
+          renderEffect(() => {
+            const [item, index] = block.s
+            node.textContent = `${index}/ ${item}`
+          })
+          return container
         },
         (item, index) => index,
       )