Reactivity not support `class`
Vue version
latest
Link to minimal reproduction
Steps to reproduce
- declare a
class
- declare a
ref
orreactive
- declare a
field
withref
inconstructor
function and declare class propertysetField
, e.g.setField(value) { this.field.value = value; }
ref
.value =new
thisclass
- call
ref
.value.setField('xxx') - throw error: Cannot create property 'value' on string
relation issue with pinia
https://github.com/vuejs/pinia/issues/1346
What is expected?
normal assignment value
What is actually happening?
throw error: Cannot create property 'value' on string
System Info
No response
Any additional comments?
No response
This is expected behavior.
- Objects wrapped by
reactive()
automatically unwraps properties that are refs. - When a class instance is wrapped by
reactive()
,this
inside its methods will be the reactive proxy.
The general rule of thumb: if you want to make something reactive, do not mix usage of its raw version and its reactive proxy. Instead, only use the reactive version.
In your case:
class Foo {
constructor() {
this.field = ''
}
setField(value) {
this.field = value;
}
}
const foo = reactive(new Foo())