Loading...
Loading...
Provides comprehensive guidance for Vue 2.x development including Options API, components, directives, lifecycle hooks, computed properties, watchers, Vuex state management, and Vue Router. Use when the user asks about Vue 2, needs to create Vue 2 components, implement reactive data binding, handle component communication, or work with Vue 2 ecosystem tools.
npx skill4agent add partme-ai/full-stack-skills vue2<template>
<div>
<p>{{ message }}</p>
<p>Count: {{ count }}</p>
<button @click="increment">Increment</button>
</div>
</template>
<script>
export default {
name: 'Counter',
data() {
return {
message: 'Hello Vue 2',
count: 0
}
},
computed: {
doubleCount() {
return this.count * 2
}
},
watch: {
count(newVal, oldVal) {
console.log(`count changed from ${oldVal} to ${newVal}`)
}
},
methods: {
increment() {
this.count++
}
},
mounted() {
console.log('Component mounted')
}
}
</script>data() {
return {
message: 'Hello',
count: 0,
user: {
name: 'Vue',
age: 2
}
}
}this.$setVue.setthis.$setcomputed: {
fullName() {
return `${this.firstName} ${this.lastName}`
}
}watch: {
// 简单监听
count(newVal, oldVal) {
// ...
},
// 深度监听
user: {
handler(newVal, oldVal) {
// ...
},
deep: true
}
}<template>
<div>
<h3>{{ title }}</h3>
<p>{{ content }}</p>
</div>
</template>
<script>
export default {
name: 'MyComponent',
props: {
title: {
type: String,
required: true
},
content: {
type: String,
default: ''
}
},
emits: ['update', 'delete'],
methods: {
handleClick() {
this.$emit('update', this.title)
}
}
}
</script>$emit$parent$children$refsimport Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [
{
path: '/',
component: () => import('./views/Home.vue')
},
{
path: '/about',
component: () => import('./views/About.vue')
}
]
const router = new VueRouter({
mode: 'history',
routes
})<template>
<div>
<router-link to="/about">About</router-link>
<router-view />
</div>
</template>
<script>
export default {
methods: {
goToAbout() {
this.$router.push('/about')
}
},
mounted() {
console.log(this.$route.params)
}
}
</script>import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
count: 0
},
getters: {
doubleCount: state => state.count * 2
},
mutations: {
INCREMENT(state) {
state.count++
}
},
actions: {
increment({ commit }) {
commit('INCREMENT')
}
}
})<script>
import { mapState, mapGetters, mapActions } from 'vuex'
export default {
computed: {
...mapState(['count']),
...mapGetters(['doubleCount'])
},
methods: {
...mapActions(['increment'])
}
}
</script>export default {
beforeCreate() {
// 实例初始化之后,数据观测之前
},
created() {
// 实例创建完成,数据观测完成
},
beforeMount() {
// 挂载开始之前
},
mounted() {
// 挂载完成
},
beforeUpdate() {
// 数据更新时,DOM 更新之前
},
updated() {
// DOM 更新完成
},
beforeDestroy() {
// 实例销毁之前
},
destroyed() {
// 实例销毁完成
}
}v-ifv-showkeyObject.freeze()$parent$children// 添加新属性
this.$set(this.user, 'age', 25)
// 修改数组索引
this.$set(this.items, 0, newItem)
// 修改数组长度
this.items.splice(newLength)