vue交互实现
Vue 交互实现方法
数据绑定
Vue 通过 v-model 实现双向数据绑定,适用于表单元素。例如:
<input v-model="message">
<p>{{ message }}</p>
data() {
return {
message: ''
}
}
事件处理
使用 v-on 或简写 @ 绑定事件:
<button @click="handleClick">点击</button>
methods: {
handleClick() {
console.log('按钮被点击');
}
}
条件渲染
v-if 和 v-show 控制元素显示:
<div v-if="isVisible">条件渲染内容</div>
<div v-show="isShow">显示控制内容</div>
列表渲染
v-for 实现列表渲染:

<li v-for="(item, index) in items" :key="index">
{{ item.text }}
</li>
计算属性
computed 处理复杂逻辑:
computed: {
reversedMessage() {
return this.message.split('').reverse().join('');
}
}
组件通信
父子组件通过 props 和 $emit 通信:
// 父组件
<child-component :msg="parentMsg" @update="handleUpdate"/>
// 子组件
props: ['msg'],
methods: {
sendMessage() {
this.$emit('update', newMsg);
}
}
状态管理
Vuex 适用于大型应用状态管理:

// store.js
const store = new Vuex.Store({
state: { count: 0 },
mutations: {
increment(state) {
state.count++;
}
}
});
异步交互
axios 处理 HTTP 请求:
methods: {
fetchData() {
axios.get('/api/data')
.then(response => {
this.data = response.data;
});
}
}
动画过渡
<transition> 组件实现动画:
<transition name="fade">
<p v-if="show">动画元素</p>
</transition>
.fade-enter-active, .fade-leave-active {
transition: opacity .5s;
}
.fade-enter, .fade-leave-to {
opacity: 0;
}
自定义指令
创建自定义指令扩展功能:
Vue.directive('focus', {
inserted(el) {
el.focus();
}
});






