1. What is Vue 3?
Vue 3 is a JavaScript framework for building user interfaces. It uses a progressive design, so you can add it to a small page or a full application.
Full lesson: Vue3 introduction
47 questions about Vue 3 with short answers and code. Each answer links to a full lesson on this site. Free, with no signup and no ads.
Vue 3 is a JavaScript framework for building user interfaces. It uses a progressive design, so you can add it to a small page or a full application.
Full lesson: Vue3 introduction
Call the createApp function with a root component. Then call the mount method with a container element.
const app = Vue.createApp({ data() { return { message: 'Hello Vue!!' } } })
app.mount('#app')
Full lesson: Vue3 intro
Import the vue.global.js file with a script tag. Then write your template inside a container element.
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
Full lesson: Vue3 install
Vite is a frontend build tool. It gives a fast development server and uses Rollup to build the final files.
npm create vite@latest
Full lesson: Vite tutorial
Vue 3 uses an HTML-based template syntax. You bind data to the DOM with directives and interpolation.
<p>{{ message }}</p>
<a :href="url">Link</a>
<button @click="handleClick">Click me</button>
Full lesson: Vue3 template syntax
Use the v-if directive on the element. Vue renders the element only when the expression is true.
<p v-if="isLoggedIn">Welcome back, user</p>
Full lesson: Vue3 v if intro
v-if removes the element from the DOM when the condition is false. v-show keeps the element in the DOM and only hides it.
<p v-if="show">A</p>
<p v-show="show">B</p>
Full lesson: Vue3 v if
Use the v-for directive on the element. Give each item a unique key value.
<ul>
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ul>
Full lesson: Vue3 v for
Use v-bind:class or the short form :class. The value can be an object that turns a class on or off.
<div :class="{ 'active': isActive }"></div>
Full lesson: Vue3 class bind
Use the v-on directive or the short form @. Set the value to a method name or an inline expression.
<button @click="count++">{{ count }}</button>
Full lesson: Vue3 v on
A Single-File Component keeps HTML, JavaScript, and CSS in one .vue file. This groups the code of one component in one place.
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>
Full lesson: Vue3 sfc
Use v-model on the input element. Vue keeps the data property and the input value in sync.
<input v-model="name">
<p>{{ name }}</p>
Full lesson: Vue3 forms
The key attribute gives each node a unique identifier. Vue uses the key to update the virtual DOM with less work.
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
Full lesson: Vue3 key
Add the ref attribute to the element. After mount, read the element from the component refs.
<input ref="myInput">
// this.$refs.myInput.focus()
Full lesson: Vue3 ref
Do not put v-if and v-for on the same element. v-if runs first and cannot see the loop variable. Put v-if on a parent element instead.
<template v-if="show">
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
</template>
Full lesson: Vue3 v if
Do not use the array index as the key when the list can change order. Use a stable and unique value such as an id.
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
Full lesson: Vue3 key
Do not change a ref value without the .value property in script code. Use count.value to read or write the value.
const count = ref(0)
count.value = count.value + 1
Full lesson: Vue3 watch
Use the v-on directive to bind an event listener. The shorthand is the @ symbol. For example, v-on:click or @click.
<button v-on:click="handleClick">Click</button>
<button @click="handleClick">Click</button>
Full lesson: Vue3 v on
Use v-model on input, textarea, and select elements. It creates two-way data binding. v-model ignores the initial value attribute and uses the data property instead.
<input v-model="message">
<p>{{ message }}</p>
Full lesson: Vue3 forms
Use the watch option to monitor changes in a reactive property. When the property changes, Vue runs the watch function. This lets you run custom logic in response.
watch: {
counter(newVal, oldVal) {
console.log(`counter changed from ${oldVal} to ${newVal}`);
}
}
Full lesson: Vue3 watch
The v-pre directive tells Vue to skip compilation for an element and its children. The element renders as raw HTML. Use it to display raw mustache tags.
<p v-pre>{{ raw }}</p>
Full lesson: Vue3 v pre
Create a file with the .vue extension. It contains template, script, and style blocks. This encapsulates the component in one file.
<template>
<div>Hello</div>
</template>
<script>
export default { name: 'Hello' }
</script>
Full lesson: Vue3 sfc
Run the npm run build command. This creates a dist directory with the production files. The dist directory contains index.html and static assets.
npm run build
Full lesson: Vue3 build
Pinia is a state management library for Vue 3. It provides a global store for shared state. Install Pinia and create a store to use it.
import { createPinia } from 'pinia'
const pinia = createPinia()
app.use(pinia)
Full lesson: Vue3 pinia
Define a mixin object with reusable options. Add it to a component's mixins array. The component merges the mixin's options with its own.
const myMixin = {
created() { this.hello() },
methods: { hello() { console.log('Hello') } }
}
const app = Vue.createApp({ mixins: [myMixin] })
Full lesson: Vue3 mixins
The inheritAttrs option controls whether non-prop attributes are applied to the root element. By default, it is true and attributes are inherited. Set it to false to prevent this.
export default {
inheritAttrs: false
}
Full lesson: Vue3 api inheritattrs
Do not use mustache syntax inside a textarea. Use v-model instead. v-model ignores the initial value and uses the data property.
<!-- Wrong -->
<textarea>{{ text }}</textarea>
<!-- Correct -->
<textarea v-model="text"></textarea>
Full lesson: Vue3 forms
You must provide a unique key for each item in a v-for loop. Without a key, Vue can reuse elements incorrectly. Always use a unique identifier.
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
Full lesson: Vue3 v for
Do not use v-if and v-for on the same element. v-if has higher priority and can cause errors. Use a computed property or a wrapper element instead.
<!-- Wrong -->
<li v-for="item in items" v-if="item.active">{{ item.name }}</li>
<!-- Correct -->
<li v-for="item in activeItems" :key="item.id">{{ item.name }}</li>
Full lesson: Vue3 v if intro
The ref attribute is not reactive. Do not use it for data binding. Use it only to access DOM elements or component instances.
<!-- Wrong -->
<input ref="inputValue">
<!-- Correct -->
<input v-model="inputValue">
Full lesson: Vue3 ref
Do not watch a property that does not exist. The watch function will not run. Ensure the property is reactive and defined in data or setup.
// Wrong: 'nonExistent' is not defined
watch: {
nonExistent() { console.log('never called') }
}
Full lesson: Vue3 watch
Do not forget to define the method in the component. If the method is missing, the event handler will fail. Always define methods in the methods option or setup function.
<!-- Wrong: handleClick is not defined -->
<button @click="handleClick">Click</button>
Full lesson: Vue3 v on
The v-pre directive tells Vue to skip compilation for an element and its children. Vue renders the content as raw HTML without template syntax processing.
<p v-pre>{{ message }}</p>
Full lesson: Vue3 v pre
Use v-on:click or the shorthand @click to listen to a click event. The handler can be a method name or an inline statement.
<button @click="counter += 1">Add 1</button>
Full lesson: Vue3 events
Define the method in the component options. Then use @click with the method name.
<button @click="greet">Click</button>
Full lesson: Vue3 events
Add a watch object to the component options. Each key is a data property name and each value is a handler function.
watch: { counter(newVal, oldVal) { console.log(newVal) } }
Full lesson: Vue3 watch
A mixin is an object that contains reusable component options. When a component uses a mixin, Vue merges the mixin options into the component options.
const myMixin = { methods: { hello() { console.log('Hello') } } }
Full lesson: Vue3 mixins
Use the mixins option and pass an array of mixin objects. Vue merges the mixin options with the component options.
const app = Vue.createApp({ mixins: [myMixin] })
Full lesson: Vue3 mixins
The component method takes priority. Vue merges the options and the component's own method overrides the mixin method.
Full lesson: Vue3 mixins
Add the v-pre directive to the element. Vue then renders the element and its children as raw HTML.
<div v-pre><p>{{ raw }}</p></div>
Full lesson: Vue3 v pre
Use the v-pre directive on the element that contains the tags. Vue does not compile the content, so the tags show as text.
<p v-pre>{{ message }}</p>
Full lesson: Vue3 v pre
A common mistake is to expect Vue to update the content inside a v-pre element. Vue skips compilation, so the content stays static.
Full lesson: Vue3 v pre
A common mistake is to write @click="greet()" when you only need the method reference. Both work, but @click="greet" is simpler and avoids extra calls.
<button @click="greet">Click</button>
Full lesson: Vue3 events
A common mistake is to watch an object and expect deep changes to trigger the handler. You must set deep: true to watch nested properties.
watch: { user: { handler() {}, deep: true } }
Full lesson: Vue3 watch
A common mistake is to assume that mixin methods override component methods. The component's own options take priority.
Full lesson: Vue3 mixins
A common mistake is to put v-pre on a parent and expect child components to work. Vue skips compilation for all children, so child components do not render.
Full lesson: Vue3 v pre
A common mistake is to write @click without a handler. The shorthand needs a method name or an inline statement after the equals sign.
<button @click="doSomething">Go</button>
Full lesson: Vue3 events