Categories
TypeScript Answers

How to use computed properties in Vue.js with TypeScript?

Spread the love

To use computed properties in Vue.js with TypeScript, we can add getters.

For instance, we write

<template>
  <div>
    <input type="text" name="Test Value" id="" v-model="text" />

    <label>{{ label }}</label>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Watch } from "vue-property-decorator";

@Component({})
export default class About extends Vue {
  private text = "test";

  get label() {
    return this.text;
  }
}
</script>

to add the label getter that returns the value of the this.text reactive property.

Conclusion

To use computed properties in Vue.js with TypeScript, we can add getters.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *