javascript - Would watch() in Vue3 stop automatically at component unmounted? - Stack Overflow

According to Vue3 Doc, watchEffect will stop when a ponent is unmounted.When watchEffect is called duri

According to Vue3 Doc, watchEffect will stop when a ponent is unmounted.

When watchEffect is called during a ponent's setup() function or lifecycle hooks, the watcher is linked to the ponent's lifecycle and will be automatically stopped when the ponent is unmounted.

Does watch has the same behavior in term of automatically stoppage? Thanks!

According to Vue3 Doc, watchEffect will stop when a ponent is unmounted.

When watchEffect is called during a ponent's setup() function or lifecycle hooks, the watcher is linked to the ponent's lifecycle and will be automatically stopped when the ponent is unmounted.

Does watch has the same behavior in term of automatically stoppage? Thanks!

Share Improve this question asked Apr 12, 2021 at 2:02 mannokmannok 1,8721 gold badge22 silver badges38 bronze badges 4
  • 2 It seems so. See v3.vuejs/guide/…. – User 28 Commented Apr 12, 2021 at 4:45
  • @User28 It did mention watch shares behavior with watchEffect in terms of manual stoppage. Seems for manual stoppage but not automatically stoppage – mannok Commented Apr 12, 2021 at 6:30
  • 4 Umm, I was confused too. The manual stoppage links to the Stopping the Watcher which talk about automatically stopped. I create this to test. It seems that the watcher automatically stopped when the ponent is unmounted. – User 28 Commented Apr 12, 2021 at 10:02
  • @User28 This is the answer. Thanks for you fiddle. – mannok Commented Apr 12, 2021 at 10:29
Add a ment  | 

1 Answer 1

Reset to default 4

Watchers declared synchronously inside setup() or are bound to the owner ponent instance, and will be automatically stopped when the owner ponent is unmounted. In most cases, you don't need to worry about stopping the watcher yourself.

Src: https://vuejs/guide/essentials/watchers.html#stopping-a-watcher

So the answer seems like, yes if you declare the watcher synchronously, and no if you declare it asynchronously.

It looks like watch()/watchEffect() return unwatch() functions that you have to call to stop asynchronously created watchers.

PS
This was mainly just a follow-up on @User28. The link in your ment was outdated but I couldn't edit it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744280989a4566568.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信