Why does vue.js not update the dom with datepicker using moment.js

后端 未结 3 923
礼貌的吻别
礼貌的吻别 2020-12-11 06:07

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

3条回答
  •  [愿得一人]
    2020-12-11 06:40

    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.

提交回复
热议问题