0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Vue3.5のwatchやライフサイクルフックの実行タイミング

Last updated at Posted at 2025-02-25

はじめに

Vueのライフサイクルフックが呼ばれる順番はここで案内されている。
https://ja.vuejs.org/guide/essentials/lifecycle.html

watchは即時ウォッチャーと
https://ja.vuejs.org/guide/essentials/watchers.html#eager-watchers

遅延ウォッチャーなどがある。
https://ja.vuejs.org/guide/essentials/watchers.html#post-watchers

これらの実行タイミングは明記されていない。
Vue Playgroundで試してみた

結論

まとめるとこう。

コンポーネントがインスタンス化される(v-ifがtrueになるなど)

watch { immediate: true }(flushの値は関係ない)

onBeforeMount

onMounted

refの値を変更する

watch: { flush: "sync" }

watch: { flush: "pre" }(デフォルトの挙動)

onBeforeUpdate

watch: { flush: "post" }

onUpdated

おわりに

onMountedした直後に必ず1回watchコールバックを実行する方法を探していたけど、なさそうだった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?