I am writing a (very) simple datepicker control in vue.js, using moment.js for formatting and mutating the date.
The problem I\'m having is that even though the date
This is because moment and vue both rely on defining objects' getters and setters, which results in one overriding another in your case. (So changing the reference works because vm.selectedDate.set
is overridden while vm.set
is not)
From moment's doc:
Moment.js uses overloaded getters and setters.
From vue's doc:
Vue will recursively convert its properties into getter/setters to make it “reactive”. The object must be plain: native objects such as browser API objects and prototype properties are ignored. A rule of thumb is that data should just be data - it is not recommended to observe objects with its own stateful behavior.`
A quick validation: I added console.log(this.selectedDate.get)
to the fiddle saurabh created, the logged function is stringGet
in moment.js
, I then checked vm.foo
's get
in my project, it's proxyGetter
which is a wrapper for reactiveGetter
, both in vue.runtime.common.js
.