---
title: Vue
date: 2025-06-13 14:45:00
background: bg-[#43CF96]
tags:
- vue
- web
categories:
- Programming
intro: |
A Vue 3 cheat sheet with the most important concepts, reactivity, component system, routing, and more. Updated for the latest version and perfect for both beginners and advanced users.
plugins:
- copyCode
- runCode
---
## ๐ Vue.js 3 Cheatsheet โ Beginner to Advanced
> The ultimate reference for mastering Vue 3.
## โ๏ธ 1. Setup
### CDN (Quick Start)
```html
{{ message }}
```
### Vite + Vue
```bash
npm create vite@latest my-vue-app --template vue
cd my-vue-app
npm install
npm run dev
```
## ๐ง 2. App Structure
```
src/
โโ components/
โโ views/
โโ App.vue
โโ main.js
```
## ๐ฆ 3. Data, Methods, Template
```js
data() {
return {
count: 0,
message: "Welcome!"
};
},
methods: {
increment() {
this.count++;
}
}
```
```html
{{ message }}
+
```
## ๐งฐ 4. Directives
| Directive | Purpose |
| --------------- | ----------------------- |
| `v-bind` / `:` | Bind attributes |
| `v-model` | Two-way binding |
| `v-if / v-else` | Conditional rendering |
| `v-show` | Toggle visibility |
| `v-for` | List rendering |
| `v-on` / `@` | Event handling |
| `v-slot` | Named/Scoped slot usage |
### Example
```html
Hi, {{ name }}!
```
## ๐ช 5. Lifecycle Hooks
```js
created() {},
mounted() {},
updated() {},
unmounted() {}
```
## ๐ฏ 6. Events
```html
Click
```
## ๐ 7. Computed & Watch
```js
computed: {
reversed() {
return this.message.split('').reverse().join('');
}
},
watch: {
count(newVal, oldVal) {
console.log(`Count changed from ${oldVal} to ${newVal}`);
}
}
```
## ๐งฑ 8. Components
### Register + Use
```js
app.component("Greeting", {
props: ["name"],
template: `Hello, {{ name }}! `
});
```
```html
```
## ๐ 9. Props & Emits
### Props
```js
props: {
title: String,
age: {
type: Number,
default: 18
}
}
```
### Emit
```js
this.$emit("custom-event", payload);
```
## ๐ 10. v-model with Components
```js
props: ['modelValue'],
emits: ['update:modelValue']
```
```html
```
## โ 11. Composition API
```js
import { ref, computed } from "vue";
export default {
setup() {
const count = ref(0);
const double = computed(() => count.value * 2);
const increment = () => count.value++;
return { count, double, increment };
}
};
```
## ๐ 12. Vue Router
```bash
npm install vue-router
```
### router.js
```js
import { createRouter, createWebHistory } from "vue-router";
import Home from "./views/Home.vue";
import About from "./views/About.vue";
const routes = [
{ path: "/", component: Home },
{ path: "/about", component: About }
];
export default createRouter({
history: createWebHistory(),
routes
});
```
### main.js
```js
import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
const app = createApp(App);
app.use(router).mount("#app");
```
## ๐ฆ 13. Pinia (Vuex Alternative)
```bash
npm install pinia
```
### store/counter.js
```js
import { defineStore } from "pinia";
export const useCounterStore = defineStore("counter", {
state: () => ({ count: 0 }),
actions: {
increment() {
this.count++;
}
}
});
```
```js
const counter = useCounterStore();
counter.increment();
```
## ๐จ 14. Slots
```html
```
## ๐งช 15. Testing
### Vitest + Vue Test Utils
```bash
npm install vitest @vue/test-utils
```
```js
import { mount } from "@vue/test-utils";
import MyComponent from "@/components/MyComponent.vue";
test("renders", () => {
const wrapper = mount(MyComponent);
expect(wrapper.text()).toContain("Hello");
});
```
## ๐งผ 16. Best Practices
* Use `ref()` for primitives, `reactive()` for objects
* Use `