vuejs / vue

This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
http://v2.vuejs.org
MIT License
207.95k stars 33.68k forks source link

When a getter is defined that does not define a setter, no recursive reactive is made. #8494

Open ts0307 opened 6 years ago

ts0307 commented 6 years ago

Version

2.5.17-beta.0

Reproduction link

https://jsfiddle.net/ts0307/pd8zr3sk/

Steps to reproduce

Run JSFiddle snippet

What is expected?

I expect, the result is shown as {"bar": "b"} instead of {"bar": "a"}

What is actually happening?

if ((!getter || setter) && arguments.length === 2) { val = obj[key] }

let childOb = !shallow && observe(val)

My example is that the data object defines that the getter does not define a setter, causing the above judgment to fail, val is not evaluated, and no recursive reactive is made.

pkaminski commented 6 years ago

FYI: Ignoring getters was done on purpose due to #7280. I added an exception for getter/setter pairs in #7828 to fix my own use case. I don't know whether ignoring pure getters is right or not, but either way it's clear that somebody will be unhappy.

ts0307 commented 6 years ago

It seems that it is difficult to be compatible with all use cases.

malaxiannv commented 5 years ago

@ts0307 if a getter is defined, proving that the property is an accessor property. If an accessor property does not have a setter, that means the property cannot be set new Value, thus there is no need to make it be recursive reactive.

shengrongchun commented 5 years ago

why not? let childOb = !shallow && observe(val) --> get: function reactiveGetter () { const value = getter ? getter.call(obj) : val; childOb = !shallow && observe(value);