Subscribe on changes!

Ref accessed via `Object.prototype.hasOwnProperty.call` isn't traced in `computed`

avatar
Sep 9th 2023

Is there anyway to tell the runtime tracer that ALL of the property, including NEW ones are used in computed?

avatar
Sep 9th 2023

I don't know why, but adding console.log(dict.value); helps in vue sfc playground, but doesn't work with vite development server (just create a fresh new vite project and paste the exact code there). SFC Playground: image

Vite: image

avatar
Sep 10th 2023

before

if (Object.prototype.hasOwnProperty.call(dict.value, i.toString())) {
      ret.push(dict.value[i.toString()] + i * 100);
}

after

 if (dict.value.hasOwnProperty(i.toString())) {
      ret.push(dict.value[i.toString()] + i * 100);
 }

You can directly use dict.value.hasOwnProperty to solve this problem

avatar
Sep 10th 2023

Thanks! Then I guess the rest of the problem is:

  1. Is this a bug, or it isn't fixable? Is this guranteed in vue's reactivity?
  2. If this isn't a bug, should this be documented in somewhere like here? Will other Object.prototype.xxxxx work? I vaguely remembered that vue can't track dynamically added properties (back in vue2) and I googled for this behavior for hours and got nothing.
avatar
Sep 10th 2023

object.hasOwnProperty works normally because vue handles it internally image

image

You can also solve this problem by setting a new object to dict.value

image
avatar
Sep 10th 2023

You can see this PR for some detailed information。#2621

avatar
Sep 26th 2023

Thank you @chenfan0. Now I understand why this doesn't work. However, SHOULD this work? Is current behavior considered a bug?

  1. If it should work, we should fix this
  2. If it shouldn't work (i.e. a 'feature' rather than a 'bug'), we should clarify this in the documentation (like in the FAQ section of reactivity page).