Last active
March 24, 2025 12:45
-
-
Save Kcko/6381cb35eb8a053634750f1a2ac4bfa8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
onScopeDispose - Podobně jako onWatcherCleanup, ale obecnější. | |
Zavolá se, když je aktuální reaktivní efektový scope (effect scope) ukončen. | |
Je to užitečné pro čištění zdrojů v jakémkoliv reaktivním kontextu: | |
*/ | |
// 1 | |
import { onScopeDispose, effectScope } from 'vue' | |
// Vytvoření izolovaného scope | |
const scope = effectScope() | |
scope.run(() => { | |
// Kód uvnitř scope | |
onScopeDispose(() => { | |
// Tento kód se zavolá při scope.stop() | |
}) | |
}) | |
// Později můžete ukončit scope | |
scope.stop() | |
// 2) | |
const scope = effectScope() | |
scope.run(() => { | |
const state = reactive({ count: 0 }) | |
const double = computed(() => state.count * 2) | |
watch(() => state.count, (count) => console.log(count)) | |
}) | |
// Později ukončí všechny reaktivní efekty | |
scope.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment