Getting Started
3 snippetsVue 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 snippetsReactive 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 snippetsTemplate 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.
Components
4 snippetsComponent 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 snippetsComponent 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 snippetsReusable 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 snippetsRouting 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 snippetsGlobal 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>