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

后端 未结 3 912
礼貌的吻别
礼貌的吻别 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:34

    The issue is that the reference to the selectedDate never changes. It is the same object even if you are calling methods on it.
    One approach here would be to store the date in a different format. As a date object, as a number etc. Then when you are making a change on it you can generate a new instance of that data.

    Here is an example:

    var datePicker = Vue.component("datePicker", {
        data: function() {
            return { selectedDate: moment().toDate() }
        },
        template: `
    {{formattedSelectedDate}} {{selectedDate}}
    `, methods: { increment: function () { var currentDate = moment(this.selectedDate); currentDate.add(1, "days"); this.selectedDate = currentDate.toDate(); }, decrement: function () { var currentDate = moment(this.selectedDate); currentDate.subtract(1, "Days"); this.selectedDate = currentDate.toDate(); } }, computed: { formattedSelectedDate: function() { return moment(this.selectedDate).format("YYYY-MM-DD"); } } }); var PointTracker = new Vue({ el: "#PointTracker", data: { date: 0 }, components: { datePicker } });

提交回复
热议问题