Subscribe on changes!

[@vue/compiler-sfc] "xxx" is a destructured prop and should not be passed directly to watch()

avatar
Apr 3rd 2023

Vue version

3.3.0-alpha.6 3.3.0-alpha.7

Link to minimal reproduction

https://sfc.vuejs.org/#eNp9kEtPwzAQhP/KyhdaKQ+kShxCW5UjF4R69sVNNiQQP1jb7aHKf8d2GxSVikMem/mcmZ0zezGmOHpkFVvbmnrjwKLzZssVQC+NJgdnsJ0YBn3aY5vBSbi6gxFa0hIewtEHriJca2Uj6y3SaxOADTTY9grfSRu7mIQK3rw8IMG4jKfCxV3rVe16rSKyx2+P1i2WcI46AIU4pKYJoBFOVLNEi2U2SUik6a42xke6zaMmPiWdGz9f9kl7LhKSQYiz2cYM6T8RWZeXukJRYXAozSAcbtfl7ytXLGOXCnMpTPFptQo1p0X4VbCcVdNqnIUy48xZ55yxVVl6Zb4+ilrLche0krxyvcS80XK3KlbFYy4G04niqWx66+ZygVbmB9Kn0Hkw5uzaQ/Iow8cjUk6oGiSkfz1v2Hu+N8gf72v3Ixt/AMad00k=

Steps to reproduce

Open reproduction url and select vue version 3.3.0-alpha.6 or alpha.7 (for some reason SFC Playground doesn't remember the selected version)

What is expected?

Should compile vue component without error since all code is valid.

What is actually happening?

Compiler throws an error:

Error: [@vue/compiler-sfc] "error" is a destructured prop and cannot be directly watched. Use a getter () => error instead.

App.vue
13 |    const { error } = useRequest();
14 |  
15 |    watch(error, () => {
   |          ^^^^^
16 |  
17 |    });

But if you remove const { userId } = defineProps({ userId: Number }) then the error is gone.

avatar
Apr 4th 2023

According to the @vue/compiler-sfc error message, you can use it like this

const { error } = useRequest();
watch(() => error, () => {
});
avatar
Apr 4th 2023

I think maybe this is a bug. When the compiler checks watch and toRef, it also checks variables that are not props destructured, but these variables should not be checked. And the behavior it exhibits is inconsistent with previous versions, which is enough to show that it is a bug wdyt? @sxzz 👀