vuejs
  1. vuejs-ts-with-composition-api

Vue.js TypeScript with Composition API

Vue.js TypeScript with Composition API is a powerful combination for building scalable and maintainable web applications. TypeScript is a statically typed superset of JavaScript that provides a way to write safer and more resilient code. The Composition API in Vue.js 3 is a new way of writing Vue component logic that allows for more flexibility, organization, and reusability.

Syntax

import { defineComponent, ref, computed } from 'vue'

export default defineComponent({
  name: 'MyComponent',
  setup() {
    const count = ref(0)
    const increment = () => {
      count.value++
    }
    const doubled = computed(() => count.value * 2)

    return {
      count,
      increment,
      doubled
    }
  },
})

Example

<template>
  <div>
    <p>Count: {{ count }}</p>
    <button @click="increment">Increment</button>
    <p>Doubled: {{ doubled }}</p>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed } from 'vue'

export default defineComponent({
  name: 'MyComponent',
  setup() {
    const count = ref(0)
    const increment = () => {
      count.value++
    }
    const doubled = computed(() => count.value * 2)

    return {
      count,
      increment,
      doubled
    }
  },
})
</script>

Output

The output of the above example will be a web page showing a count and a button to increment the count. The page will also show the doubled value of the count.

Explanation

In the example code, we are using the Composition API to define a Vue.js component with TypeScript. The defineComponent() function is used to define the component and the setup() function is used to set up the component's logic.

Within the setup() function, three variables are declared: count, increment, and doubled. The count variable is declared as a ref with an initial value of 0. increment is declared as a function that will increment the count value by one. doubled is declared as a computed property that doubles the count value.

The return statement in the setup() function returns an object with the count, increment, and doubled variables, which can be accessed in the template section of the component.

Use

The Composition API provides a way to organize complex component logic into reusable code blocks. It allows for more control over component state and provides a way to encapsulate complex logic. TypeScript adds static typing to the mix, which can help catch errors earlier and make the codebase more maintainable.

Important Points

  • The Composition API is only available in Vue.js 3 and above.
  • TypeScript is a superset of JavaScript that provides static typing and enhanced features to improve the safety and scalability of JavaScript code.
  • The ref function and computed property are two pre-built options for building reactive data sources.
  • The defineComponent() function is used to define new Vue components using the Composition API.

Summary

Vue.js with TypeScript and the Composition API is a powerful combination for building scalable and maintainable web applications. The Composition API provides more flexibility and organization in component logic, while TypeScript provides static typing and enhanced features to improve the safety and scalability of the code. Understanding these concepts can greatly enhance your Vue.js development capabilities.

Published on: