AI Generate Vue docs instantly

Vue Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

3 snippets

Vue 3 basics

Create App

import { createApp } from 'vue'
import App from './App.vue'

const app = createApp(App)
app.mount('#app')

Template Syntax

<template>
  <div>
    <h1>{{ message }}</h1>
    <p>Count: {{ count }}</p>
  </div>
</template>

Comments

<template>
  <!-- HTML comment -->
  <div>Content</div>
</template>

<script>
// JavaScript comment
</script>

Reactivity (Composition API)

5 snippets

Reactive state

ref

import { ref } from 'vue'

const count = ref(0)
count.value++
console.log(count.value)  // 1

reactive

import { reactive } from 'vue'

const state = reactive({
  count: 0,
  name: 'Vue'
})

state.count++

computed

import { ref, computed } from 'vue'

const count = ref(0)
const doubled = computed(() => count.value * 2)

console.log(doubled.value)  // 0

watch

import { ref, watch } from 'vue'

const count = ref(0)

watch(count, (newVal, oldVal) => {
  console.log(`Changed from ${oldVal} to ${newVal}`)
})

watchEffect

import { ref, watchEffect } from 'vue'

const count = ref(0)

watchEffect(() => {
  console.log(count.value)  // Runs immediately and on change
})

Directives

6 snippets

Template directives

v-bind (Attributes)

<img v-bind:src="imageSrc" />
<!-- Shorthand -->
<img :src="imageSrc" :alt="imageAlt" />

v-on (Events)

<button v-on:click="handleClick">Click</button>
<!-- Shorthand -->
<button @click="count++">Increment</button>

v-model (Two-way Binding)

<input v-model="message" />
<p>{{ message }}</p>

v-if / v-else

<div v-if="isLoggedIn">
  Welcome back!
</div>
<div v-else>
  Please log in
</div>

v-show

<div v-show="isVisible">
  This toggles display CSS
</div>

v-for

<ul>
  <li v-for="(item, index) in items" :key="item.id">
    {{ index }}: {{ item.name }}
  </li>
</ul>

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Components

4 snippets

Component structure

Single File Component

<template>
  <div>
    <h1>{{ title }}</h1>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const title = ref('Hello Vue')
</script>

<style scoped>
h1 {
  color: blue;
}
</style>

Props

<script setup>
defineProps({
  title: String,
  count: {
    type: Number,
    required: true,
    default: 0
  }
})
</script>

Emits

<script setup>
const emit = defineEmits(['update', 'delete'])

function handleUpdate() {
  emit('update', { id: 1 })
}
</script>

Use Component

<template>
  <MyComponent
    :title="pageTitle"
    :count="10"
    @update="handleUpdate"
  />
</template>

<script setup>
import MyComponent from './MyComponent.vue'
</script>

Lifecycle Hooks

4 snippets

Component lifecycle

onMounted

import { onMounted } from 'vue'

onMounted(() => {
  console.log('Component mounted')
})

onUpdated

import { onUpdated } from 'vue'

onUpdated(() => {
  console.log('Component updated')
})

onUnmounted

import { onUnmounted } from 'vue'

onUnmounted(() => {
  console.log('Component unmounted')
})

onBeforeMount

import { onBeforeMount } from 'vue'

onBeforeMount(() => {
  console.log('Before mount')
})

Composables

2 snippets

Reusable composition functions

Create Composable

// useCounter.js
import { ref } from 'vue'

export function useCounter() {
  const count = ref(0)
  const increment = () => count.value++

  return { count, increment }
}

Use Composable

<script setup>
import { useCounter } from './useCounter'

const { count, increment } = useCounter()
</script>

<template>
  <button @click="increment">{{ count }}</button>
</template>

Vue Router

3 snippets

Routing basics

Define Routes

import { createRouter, createWebHistory } from 'vue-router'
import Home from './Home.vue'
import About from './About.vue'

const routes = [
  { path: '/', component: Home },
  { path: '/about', component: About }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

Router Links

<template>
  <router-link to="/">Home</router-link>
  <router-link to="/about">About</router-link>
  <router-view />
</template>

Programmatic Navigation

import { useRouter } from 'vue-router'

const router = useRouter()

function goToAbout() {
  router.push('/about')
}

Pinia (State Management)

2 snippets

Global state with Pinia

Define Store

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useCounterStore = defineStore('counter', () => {
  const count = ref(0)
  const doubled = computed(() => count.value * 2)
  const increment = () => count.value++

  return { count, doubled, increment }
})

Use Store

<script setup>
import { useCounterStore } from '@/stores/counter'

const counter = useCounterStore()
</script>

<template>
  <div>{{ counter.count }}</div>
  <button @click="counter.increment">Increment</button>
</template>

More Cheat Sheets

FAQ

Frequently asked questions

What is a Vue cheat sheet?

A Vue cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in Vue. It helps developers quickly look up syntax without searching through documentation.

How do I learn Vue quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important Vue concepts?

Key Vue concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my Vue code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your Vue code using AI.

Code Conversion Tools

Convert Vue to Other Languages

Easily translate your Vue code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate Vue Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week