Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue Tutorial

Vue HOME Vue Intro Vue Directives Vue v-bind Vue v-if Vue v-show Vue v-for Vue Events Vue v-on Vue Methods Vue Event Modifiers Vue Forms Vue v-model Vue CSS Binding Vue Computed Properties Vue Watchers Vue Templates

Scaling Up

Vue Why, How and Setup Vue First SFC Page Vue Components Vue Props Vue v-for Components Vue $emit() Vue Fallthrough Attributes Vue Scoped Styling Vue Local Components Vue Slots Vue v-slot Vue Scoped Slots Vue Dynamic Components Vue Teleport Vue HTTP Request Vue Template Refs Vue Lifecycle Hooks Vue Provide/Inject Vue Routing Vue Form Inputs Vue Animations Vue Animations with v-for Vue Build Vue Composition API

Vue Reference

Vue Built-in Attributes Vue Built-in Components Vue Built-in Elements Vue Component Instance Vue Directives Vue Instance Options Vue Lifecycle Hooks

Vue Examples

Vue Examples Vue Exercises Vue Quiz Vue Server Vue Certificate

Vue Composition API

The Composition API is an alternative way of writing Vue applications to the Options API that is used elsewhere in this tutorial.

In Composition API we can write code more freely, but it requires a deeper understanding, and it is considered to be less beginner-friendly.

The Composition API

With Composition API, logic is written using imported Vue functions instead of using the Vue instance structure that we are used to from Options API.

This is how Composition API can be used to write a Vue application that decreases the number of typewriters in storage with a button:

Example

App.vue:

<template>
  <h1>Example</h1>
  <img src="/img_typewriter.jpeg" alt="Typewriter">
  <p>Typewriters left in storage: {{ typeWriters }}</p>
  <button @click="remove">Remove one</button>
  <p style="font-style: italic;">"{{ storageComment }}"</p>
</template>

<script setup>
  import { ref, computed } from 'vue'

  const typeWriters = ref(10);

  function remove(){
    if(typeWriters.value>0){
      typeWriters.value--;
    }
  }

  const storageComment = computed(
    function(){
      if(typeWriters.value > 5) {
        return "Many left"
      }
      else if(typeWriters.value > 0){
        return "Very few left"
      }
      else {
        return "No typewriters left"
      }
    }
  )
</script>
Run Example »

On line 9 in the example above, the setup attribute makes it easier to use Composition API. For example, by using the setup attribute, variables and functions can be used directly inside the <template>.

On line 10, ref and computed must be imported before they can be used. In Options API, we do not need to import anything to declare reactive variables or to use computed properties.

On line 12, ref is used to declare the 'typewriters' property as reactive with '10' as the initial value.

To declare the 'typewriters' property as reactive means that the line {{ typewriters }} in the <template> will be re-rendered automatically to show the updated value when the 'typewriters' property value is changed. With Option API, data properties become reactive if they need to be when the application is built, they do not need to be declared explicitly as reactive.

The 'remove()' function declared on line 14 would be declared under the Vue property 'methods' if the example was written in Options API.

The 'storageComment' computed property on line 20 would be declared under the Vue property 'computed' if the example was written in Options API.


The Options API

The Options API is what is used elsewhere in this tutorial.

The Options API is chosen for this tutorial because it has a recognizable structure and is considered easier to start with for beginners.

As an example, the structure in the Options API has the data properties, methods and computed properties all placed in different parts of the Vue instance, clearly separated.

Here is the example above written with Options API:

Example

App.vue:

<template>
  <h1>Example</h1>
  <img src="/img_typewriter.jpeg" alt="Typewriter">
  <p>Typewriters left in storage: {{ typeWriters }}</p>
  <button @click="remove">Remove one</button>
  <p style="font-style: italic;">"{{ storageComment }}"</p>
</template>

<script>
export default {
  data() { 
    return {
      typeWriters: 10
    };
  },
  methods: {
    remove(){
      if(this.typeWriters>0){
        this.typeWriters--;
      }
    }
  },
  computed: {
    storageComment(){
      if(this.typeWriters > 5) {
        return "Many left"
      }
      else if(this.typeWriters > 0){
        return "Very few left"
      }
      else {
        return "No typewriters left"
      }
    }
  }
}
</script>
Run Example »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.