示例工程 24-vue3-rewrite

24-vue3-rewrite/demo-02-scope/scope.ts

typescript · 0.6 KB

import { effectScope, onScopeDispose, ref, watch } from "vue";

const count = ref(0);
const log: string[] = [];

const scope = effectScope();
scope.run(() => {
  watch(
    count,
    (value) => {
      log.push(`watch:${value}`);
    },
    { flush: "sync" },
  );
  onScopeDispose(() => {
    log.push("dispose");
  });
});

count.value = 1;
count.value = 2;
scope.stop();
count.value = 3;

console.log(
  JSON.stringify({
    evt: "smoke",
    ok:
      log.join(",") === "watch:1,watch:2,dispose" &&
      count.value === 3 &&
      log.length === 3,
    log,
    count: count.value,
  }),
);