Why use getter for functions that use Moment.js? (vueJS, typescript)

Viewed 51

weekOfMonth() returns this month and this week(week of this month)

<template>
  <h3>{{ weekOfMonth }}</h3>
</template>

<script lang="ts">
export default class HomeView extends Vue {
  
  const moment = require("moment");
  
  private get weekOfMonth(): string {
    const nowDate = moment().utc(true);
    let week: number = nowDate.week() - moment(nowDate).startOf("month").week() + 1;
    return `This Month: ${nowDate.month() + 1} / This Week: ${week}`;
  }
}
</script>

result: This Month: 7 / This Week: 4

result when not using 'get' : function () { [native code] }

Why do I have to use getter to get the return value?

1 Answers

the getter is called when you use it. if you are trying to use the normal function, you need call it with weekOfMonth(), so try add parentheses like below

<template>
  <h3>{{ weekOfMonth() }}</h3>
</template>
Related