I'm new to vue.js and I'm wondering why the following code is not working as expected:
<template>
<page-layout>
<h1>Hello, Invoicer here</h1>
<form class="invoicer-form">
<div><label><span>Datum</span><input v-model="date" v-on:change="dateChanged" /></label></div>
<div><label><span>Zeitraum</span><input v-model="timespan" /></label></div>
</form>
</page-layout>
</template>
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator'
import PageLayout from '@/components/layout/PageLayout.vue'
import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat'
@Component({
components: { PageLayout }
})
export default class Invoicer extends Vue {
date = ''
_timespan = ''
beforeCreate(): void {
dayjs.extend(customParseFormat)
}
dateChanged(): void {
const format = 'DD.MM.YYYY'
const date = dayjs(this.date, format)
if (date.isValid()) {
if (!this.timespan) {
const from = date.subtract(1, 'month').startOf('month').format(format)
const until = date.endOf('month').format(format)
this.timespan = `${from} - ${until}`
}
}
}
get timespan(): string {
return this._timespan
}
set timespan(value: string) {
this._timespan = value
}
}
</script>
when I change the 'Datum' the dateChanged()-method is executed and sets the _timespan-property with its setter. But the GUI won't be updated. If I remove the setter/getter and use the _timespan`-property directly, everything works fine. I really think it should also work with the setter/getter or in other therms, computed property, shouldn't it?