Vue.jsContext and Scope in Vue: Why
Context and Scope في Vue
افهم الفرق بين scope وcontext في Vue ولماذا يجب تشغيل watch داخل setup أو composable من Vue.
٢ أبريل ٢٠٢٦
٨ دقائق
VuewatchComposition API
Context and Scope in Vue: Why watch Must Run Inside Vue Context
Introduction
In Vue, some APIs are not just regular functions. APIs like watch need to run inside Vue's execution context so they can connect to reactivity, lifecycle, and cleanup correctly.
Scope vs context in Vue
Scopemeans which variables or refs you can accessContextmeans whether the code is running inside Vue's active component/reactivity environment
The common mistake
Writing watch() directly in a random ts file is usually the wrong abstraction:
import { watch } from 'vue';
The better pattern is to wrap it in a composable and call that composable from setup or <script setup>.
import { watch, type Ref } from 'vue';
export function useUserWatcher(userId: Ref<number | null>) {
watch(userId, (value) => {
console.log(value);
});
}
The key rule
- Defining logic in a
tsfile is fine - Executing
watchshould happen in Vue context
That usually means:
setup()<script setup>- A composable called from setup
Summary
If you want to use watch in a TypeScript file, put it inside a function or composable, then call that function from Vue's setup context. That way Vue can properly register, manage, and clean up the watcher.