当前位置: 首页 > article >正文

Vue组件通信方式及最佳实践

1. Props / 自定义事件 (父子通信)

使用场景

父子组件直接数据传递

代码实现

<!-- Parent.vue -->
<template><Child :message="parentMsg" @update="handleUpdate" />
</template><script setup>
import { ref } from 'vue';
import Child from './Child.vue';const parentMsg = ref('Hello from Parent');
const handleUpdate = (newVal) => {parentMsg.value = newVal;
};
</script><!-- Child.vue -->
<template><div><p>{{ message }}</p><button @click="sendUpdate">Update Parent</button></div>
</template><script setup>
const props = defineProps(['message']);
const emit = defineEmits(['update']);const sendUpdate = () => {emit('update', 'New value from Child');
};
</script>

使用步骤

  1. 父组件通过 :propName 传递数据
  2. 子组件通过 defineProps 接收
  3. 子组件通过 defineEmits 声明事件
  4. 子组件通过 emit('eventName', data) 触发事件
  5. 父组件通过 @eventName 监听处理

关键点

  • 单向数据流原则
  • 子组件不要直接修改 props
  • 适合层级简单场景

2. v-model / .sync (双向绑定)

使用场景

简化父子组件的双向绑定

代码实现

<!-- Parent.vue -->
<template><Child v-model:title="pageTitle" /><p>Parent value: {{ pageTitle }}</p>
</template><script setup>
import { ref } from 'vue';
import Child from './Child.vue';const pageTitle = ref('Initial Title');
</script><!-- Child.vue -->
<template><input :value="title"@input="$emit('update:title', $event.target.value)">
</template><script setup>
defineProps(['title']);
defineEmits(['update:title']);
</script>

使用步骤

  1. 父组件使用 v-model:propName 绑定
  2. 子组件接收对应 prop
  3. 子组件通过 update:propName 事件更新

关键点

  • Vue 3 支持多个 v-model 绑定
  • 替代 Vue2 的 .sync 修饰符
  • 语法糖,底层仍是 props + events

3. Event Bus (全局事件总线)

使用场景

跨组件通信(小型项目)

代码实现

// eventBus.js
import mitt from 'mitt';
export const emitter = mitt();// ComponentA.vue (发送方)
import { emitter } from './eventBus';
emitter.emit('global-event', { data: 123 });// ComponentB.vue (接收方)
import { emitter } from './eventBus';
emitter.on('global-event', (data) => {console.log('Received:', data);
});

使用步骤

  1. 创建全局事件总线实例
  2. 发送方使用 emit 触发事件
  3. 接收方使用 on 监听事件
  4. 组件销毁时使用 off 移除监听

关键点

  • 需要手动管理事件监听
  • 适用于简单场景
  • 中大型项目改用状态管理

4. Provide / Inject

使用场景

跨层级组件通信

代码实现

<!-- Ancestor.vue -->
<script setup>
import { provide, ref } from 'vue';const counter = ref(0);
provide('counter', {counter,increment: () => counter.value++
});
</script><!-- Descendant.vue -->
<script setup>
import { inject } from 'vue';const { counter, increment } = inject('counter');
</script><template><button @click="increment">{{ counter }}</button>
</template>

使用步骤

  1. 祖先组件使用 provide(key, value)
  2. 后代组件使用 inject(key)
  3. 建议提供响应式数据

关键点

  • 适合深层嵌套组件
  • 提供响应式对象更实用
  • 避免组件过度耦合

5. Pinia (状态管理)

使用场景

复杂应用状态管理

代码实现

// stores/counter.js
import { defineStore } from 'pinia';export const useCounterStore = defineStore('counter', {state: () => ({ count: 0 }),actions: {increment() {this.count++;}}
});// ComponentA.vue
import { useCounterStore } from './stores/counter';
const store = useCounterStore();
store.increment();// ComponentB.vue
import { useCounterStore } from './stores/counter';
const store = useCounterStore();
<p>{{ store.count }}</p>

使用步骤

  1. 定义 store
  2. 组件导入并使用 store
  3. 通过 actions 修改状态

关键点

  • 集中式状态管理
  • 支持 TypeScript
  • 替代 Vuex 的现代方案

6. refs 访问组件实例

使用场景

需要直接操作子组件

代码实现

<template><ChildComponent ref="childRef" /><button @click="callChildMethod">Call Child</button>
</template><script setup>
import { ref } from 'vue';
import ChildComponent from './Child.vue';const childRef = ref(null);const callChildMethod = () => {childRef.value.someMethod();
};
</script>

使用步骤

  1. 使用 ref 属性标记子组件
  2. 通过 ref.value 访问实例
  3. 调用子组件方法/访问属性

关键点

  • 破坏封装性,谨慎使用
  • 优先考虑 props/events
  • 适合集成第三方库

对比总结表

方式适用场景优点缺点
Props/Events父子组件通信简单直接不适合深层嵌套
v-model双向绑定语法简洁只能用于父子组件
Event Bus跨组件通信全局可用难以维护事件流
Provide/Inject跨层级通信避免逐层传递数据来源不透明
Pinia复杂状态管理集中管理可维护性强增加项目复杂度
Refs直接访问组件灵活性强破坏组件封装

通用最佳实践

  1. 简单优先原则:优先使用 Props/Events
  2. 状态共享评估
    • 父子组件 → Props
    • 兄弟组件 → 状态提升到父级
    • 跨层级 → Provide/Inject 或 Pinia
  3. 类型安全:使用 TypeScript 定义 Props 和事件
  4. 响应式处理:对于复杂对象使用 reactive()ref()
  5. 内存管理:及时清理 Event Bus 监听器
  6. 模块化设计:Pinia Store 按功能拆分模块

http://www.lryc.cn/news/2383272.html

相关文章:

  • 【实用教程】如何快速搭建一套私有的埋点系统?
  • 深入解析 Uniswap:自动做市商模型的数学推导与智能合约架构
  • spring配置并使用rabbitmq
  • Android开发——不同布局的定位属性 与 通用属性
  • React 19版本refs也支持清理函数了。
  • Python高效网络爬虫开发指南
  • Python爬虫实战:获取国家统计网最新消费数据并分析,为从业者做参考
  • Python中使用uv创建环境及原理详解
  • 阿尔泰科技助力电厂——520为爱发电!
  • 【Golang笔记02】函数、方法、泛型、接口学习笔记
  • C#语法篇 :基类子类转换,成员变化情况
  • 【漫话机器学习系列】264.内距(又称四分位差)Interquartile Range
  • 海外盲盒系统开发:重构全球消费体验的科技引擎
  • 高噪声下扩展边缘检测算子对检测边缘的影响
  • vuejs处理后端返回数字类型精度丢失问题
  • mysql数据库-中间件MyCat
  • 手搓四人麻将程序
  • PotPlayer 安装 madVR、LAV Filters 以提升解码能力和视频音频效果
  • 阿里云域名 绑定 华为云服务器ip
  • windows7安装node18
  • Maven配置安装
  • 小刚说C语言刷题—1153 - 查找“支撑数”
  • Kind方式部署k8s单节点集群并创建nginx服务对外访问
  • K个一组链表翻转
  • Python60日基础学习打卡D32
  • 面向恶劣条件的道路交通目标检测----大创自用(当然你也可以在里面学到很多东西)
  • 基于Java(SSM)+MySQL实现(Web)具有智能推荐功能的图书销售系统
  • 浙大团队研发Earth Explorer系统,探索深时演化/地学剖面/科研场景,赋能深时地球科学研究
  • docker 启动一个python环境的项目
  • 31-35【动手学深度学习】深度学习硬件