vuejs / core

🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
https://vuejs.org/
MIT License
47.79k stars 8.35k forks source link

Warning about "write computed value failed" if <component> attr 'is' value is same about 'ref' #12437

Closed GuangTom closed 5 days ago

GuangTom commented 6 days ago

Vue version

3.5.12

Link to minimal reproduction

https://play.vuejs.org/#eNp9U8tu2zAQ/JUFUYAyoMiH9mTYQR/woT20Repb1YMrr2ymEknwIbtw9O/dpSzZDYLcyJlZ7cwudRYfrC26iGIhlr5yygbwGKK9L7VqrXEBzlCZ1saAuxwc1jlEjxtsbbMN+IA19FA704Kkj8hSl7oy2gfAVgVYwQ5rpXFNF5/9lHRW/iBzkP5gjndBWflrNlaEvxapglpkMqAPcmI6hUfutHrWOpPMsO5WSbLRcJbNYHUP51IDzOfgTYtQbT36nIxZ1DswGiQ3ltBtm4gFKx3ldzr5KRIKT08Uj1uVur/pxiE2ylLDrdv7qRNHz64JcyiKggVU2U+l8fcwoP8dkjKJlvNhF7QFuoRLZLoBLDmc0agD3wAWyq9Kwe5KMSDdnaoJYv8jREO9iGhsI/h+WAcRg+NpO2+wo8/PJt0YhZSXyFcq5WAiHQZ8TkaX8xvXIhfBU/Ba7YtHbzQ9tpS4FBxGNei+2aBoMKVYDLNgbts05vglYcFFzEe8OmD15wX80Z8YK8V3hx5dR+knLtACkOwxvf7xFU90nsjW7GJD6lfIB/SmiexxkH2Meke2b3TJ7ef0yyi93/j1KaD2Yyg2yso+6WkVET+9Ev1q923xLtXRsxD9P0TzORI=

Steps to reproduce

将 vue2 选项式写法重构为 vue3 setup 语法糖写法时,控制台出现 [Vue warn] Write operation failed: computed value is readonly 问题,事实上我并没有修改 computed 值,自我定位发现是:

使用了 <component>,其中 :is 属性和 ref 属性的值名称相同,并且用 useTemplateRef 定义了对应的模板引用

<component
      :is="view"
      v-if="type"
      ref="view"
      @finish="emit('finish', $event)"
      @show-tip="showTip"
      @submit="submit"
/>

view: computed
type: ref
const viewRef = useTemplateRef('view')

ref="view" 中的 view 应该是一个字符串,不清楚为何会与 :is="view" 的变量 view 冲突。

修改为 ref="viewRef" 后,不再出现 warning

训练场 url

What is expected?

不出现该 warning

What is actually happening?

Pages console appear warning: [Vue warn] Write operation failed: computed value is readonly

System Info

No response

Any additional comments?

No response

yangxiuxiu1115 commented 6 days ago

ref="view",会尝试给setup中的声明的同名响应式变量赋值。在上面的case中会尝试给computedRef赋值,从而导致了warning

edison1105 commented 5 days ago

@yangxiuxiu1115 说的是对的。

GuangTom commented 5 days ago

Thanks for clarifying.