composition-api: fix: [Vue warn]: Write operation failed: computed value is readonly.

🐛 The bug At the moment, if a component that calls useFetch has a computed property returned from setup, it will give the following error:

[Vue warn]: Computed property was assigned to but it has no setter.

It still works correctly, but this is unattractive, and ideally we wouldn’t be attempting to assign a value to a computed property at all.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 14
  • Comments: 23 (5 by maintainers)

Commits related to this issue

Most upvoted comments

@chovyprognos In the composition API you would do that like so:

const a = computed({
  get() {
    // your getter
  },
  set(val) {
    // your setter
  }
})

I’m getting this same warning but it’s not even a computed variable.

const page: any = reactive({
  content: null,
})

later :

const { fetch } = useFetch(async () => {
  const pageData = await (fetch stuff...)
  if (pageData) {
     page.content = pageData
  }
})

Setting page.content’s value throws the warning about write operation. Is there any way to avoid this weird warning?

@davaipoka Hm, you’re right. I had a computed that depended on two values (say countries and user addresses). The client-side render worked but the server-side didn’t work (computed value was not pre-populated). When I added the dependencies the computed relies on - countries and user addresses refs to the setup function’s return object, the server-side render started working.

Normally I wouldn’t include those 2 refs in the return object - they’re not used in the template. But if it makes it work it makes it work.

just because this is the top search result for this error. i ran into this issue when i accidentally had a ref attribute in my template that was named the same as a computed property. renaming/removing the ref fixed things up for me.

I’ll make sure to make an issue with a reproduction!