Vue 3 cheatsheet

Vue 3: Components

7 common Vue 3 tasks.

Declare props

Declare the props a component accepts, with types.

const props = defineProps<{ title: string; count?: number }>();

Declare events

Declare the events a component emits, then emit one.

const emit = defineEmits<{ (e: "update", value: string): void }>();
emit("update", "hello");

Bind a form input

Two-way bind an input to state.

<input v-model="name" />

Render a list

Loop over an array and give each item a key.

<li v-for="item in items" :key="item.id">{{ item.name }}</li>

Render conditionally

Add or remove an element depending on a condition.

<p v-if="loading">Loading...</p>
<p v-else>Done</p>

Accept child content

Render content passed in by the parent, with fallback text.

<slot>Default content</slot>

Share data with descendants

Provide a value in one component and inject it deeper in the tree.

provide("theme", "dark");
const theme = inject("theme", "light");

Was this page helpful?