Vue.js Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。
The ultimate reference for mastering Vue 3.
<script src="https://unpkg.com/vue@3"></script>
<div id="app">{{ message }}</div>
<script>
Vue.createApp({
data() {
return { message: 'Hello Vue!' };
}
}).mount('#app');
</script>
npm create vite@latest my-vue-app --template vue
cd my-vue-app
npm install
npm run dev
src/
├─ components/
├─ views/
├─ App.vue
├─ main.js
data() {
return {
count: 0,
message: "Welcome!"
};
},
methods: {
increment() {
this.count++;
}
}
<h1>{{ message }}</h1>
<button @click="increment">+</button>
| 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 |
<input v-model="name" />
<p v-if="name">Hi, {{ name }}!</p>
<ul>
<li v-for="item in list" :key="item.id">{{ item }}</li>
</ul>
created() {},
mounted() {},
updated() {},
unmounted() {}
<button @click="sayHi">Click</button> <input @keyup.enter="submit" />
computed: {
reversed() {
return this.message.split('').reverse().join('');
}
},
watch: {
count(newVal, oldVal) {
console.log(`Count changed from ${oldVal} to ${newVal}`);
}
}
app.component('Greeting', {
props: ['name'],
template: `<h1>Hello, {{ name }}!</h1>`
});
<Greeting name="Sumangal" />
props: {
title: String,
age: {
type: Number,
default: 18
}
}
this.$emit('custom-event', payload);
props: ['modelValue'],
emits: ['update:modelValue']
<input :value="modelValue" @input="$emit('update:modelValue', $event.target.value)" />
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 };
}
};
npm install vue-router
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
});
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
const app = createApp(App);
app.use(router).mount('#app');
npm install pinia
import { defineStore } from 'pinia';
export const useCounterStore = defineStore('counter', {
state: () => ({ count: 0 }),
actions: {
increment() {
this.count++;
}
}
});
const counter = useCounterStore();
counter.increment();
<!-- Default -->
<slot></slot>
<!-- Named -->
<slot name="header"></slot>
<!-- Scoped -->
<slot :user="user"></slot>
npm install vitest @vue/test-utils
import { mount } from '@vue/test-utils';
import MyComponent from '@/components/MyComponent.vue';
test('renders', () => {
const wrapper = mount(MyComponent);
expect(wrapper.text()).toContain('Hello');
});
ref() for primitives, reactive() for objects<script setup> syntax in SFCskey in v-for地址
Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)Level 2, 171 La Trobe St, Melbourne VIC 3000四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号Business Hub, 155 Waymouth St, Adelaide SA 5000Disclaimer
JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.
匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议
© 2017-2025 JR Academy Pty Ltd. All rights reserved.
ABN 26621887572