Quick Reference Vue.js Cheatsheet
Vue 3 on one page — script setup, template bindings, ref and computed, v-if and v-for,
props and emits, composables, Vue Router, Pinia, and the traps that show up in interviews.
10 sections 10 tables 17 code snippets Create and run npm create vue@latest
npm install
npm run dev
npm run build What the scaffold gives you src/main.ts app startup
src/App.vue root component
src/router/index.ts Vue Router
src/stores/ Pinia stores This page is Vue 3 with script setup. data(), methods, and this.message are the Options API. You will still see them in old code. 🧱 A single-file component Script import { ref } from 'vue'
const count = ref(0)
function inc() {
count.value += 1
} Template <button @click="inc">
Count is {{ count }}
</button> Text, attribute, event, model <p>{{ name }}</p>
<img :src="photo" :alt="name" />
<button @click="save">Save</button>
<input v-model="query" />
<p :class="{ active: isOn }">Status</p> v-html inserts raw markup. Use it only for HTML you trust. User text belongs in {{ }}, which escapes it. 📶 ref, reactive & computed Local state const count = ref(0)
const doubled = computed(() => count.value * 2)
const user = reactive({
name: 'Ravi',
city: 'Hyderabad',
}) Watch a source watch(count, (value) => {
console.log(value)
})
watch(() => user.name, (name) => {
console.log(name)
}) user.name = "Ada" updates the view. user = { name: "Ada" } does not, if user came from reactive. Hold the object in a ref when you replace it. Show, hide, repeat <p v-if="user">Hello, {{ user.name }}</p>
<p v-else>Sign in</p>
<ul>
<li v-for="item in items" :key="item.id">
{{ item.name }}
</li>
</ul> Filter in a computed list, then v-for that list. That keeps the template free of the v-if plus v-for clash. In and out const props = defineProps<{
name: string
age?: number
}>()
const emit = defineEmits<{
save: [name: string]
}>()
function onSave() {
emit('save', props.name)
} A component v-model const title = defineModel<string>({ required: true })
function rename(next: string) {
title.value = next
} Props are read-only. Changing props.name warns and the parent does not update. Emit an event, or assign to a defineModel() ref. 🪝 Composables & lifecycle A composable import { onMounted, onUnmounted, ref } from 'vue'
export function useNow() {
const now = ref(new Date())
let timer = 0
onMounted(() => {
timer = window.setInterval(() => {
now.value = new Date()
}, 1000)
})
onUnmounted(() => {
window.clearInterval(timer)
})
return { now }
} A composable is a function that owns state. Name it useSomething and call it from <script setup> so the lifecycle hooks attach to that component. Leave the page from script const router = useRouter()
const route = useRoute()
function openUser(id: number) {
router.push({ name: 'user', params: { id } })
} Links and the outlet <RouterLink to="/users">Users</RouterLink>
<RouterView /> A setup store export const useCartStore = defineStore('cart', () => {
const items = ref<string[]>([])
function add(name: string) {
items.value = [...items.value, name]
}
return { items, add }
}) Use it in a component const cart = useCartStore()
const { items } = storeToRefs(cart)
function addBook() {
cart.add('Vue')
} const { items } = useCartStore() copies the array once. Later add calls will not update items. Destructure state with storeToRefs. Script reads .value function inc() {
count.value += 1
} Template does not <button @click="inc">{{ count }}</button> How to use this page: scaffold an app with npm create vue@latest, put a ref in <script setup>, and render it. Then add a Pinia store and destructure items both ways so you can see which one updates. |