AI日报一年实战复盘:信息筛选、信源管理与稳定更新的运营方法论
2026/10/22 9:51:44
在前端开发中,组件化是提升代码复用性和可维护性的关键手段。而组件间通信则是实现组件协同工作的核心机制。本文将深入解析父子、兄弟、跨级组件通信的8种主流方式,涵盖Vue和React两大框架,帮助开发者构建高效、可维护的组件体系。
适用场景:父组件向子组件传递数据
实现原理:
:)将数据传递给子组件props选项声明接收的属性Vue示例:
<!-- 父组件 --> <template> <ChildComponent :message="parentMessage" :userInfo="userInfo" /> </template> <script setup> import { ref, reactive } from 'vue'; const parentMessage = ref('Hello from Parent'); const userInfo = reactive({ name: 'Alice', age: 25 }); </script> <!-- 子组件 --> <script setup> const props = defineProps({ message: String, userInfo: { type: Object, required: true, default: () => ({ name: 'Guest' }) } }); </script>React示例:
// 父组件 function Parent() { const [message, setMessage] = useState('Hello from Parent'); const userInfo = { name: 'Alice', age: 25 }; return <Child message={message} userInfo={userInfo} />; } // 子组件 function Child({ message, userInfo }) { return ( <div> <p>{message}</p> <p>{userInfo.name}, {userInfo.age}</p> </div> ); }适用场景:子组件向父组件传递数据
实现原理:
$emit(Vue)或回调函数(React)触发事件v-on/@或props传递的回调函数监听事件Vue示例:
<!-- 子组件 --> <script setup> const emit = defineEmits(['update']); function sendData() { emit('update', { newData: 'Updated from Child' }); } </script> <!-- 父组件 --> <template> <ChildComponent @update="handleUpdate" /> </template> <script setup> function handleUpdate(data) { console.log('Received:', data); } </script>React示例:
// 子组件 function Child({ onUpdate }) { function sendData() { onUpdate({ newData: 'Updated from Child' }); } return <button onClick={sendData}>Update Parent</button>; } // 父组件 function Parent() { function handleUpdate(data) { console.log('Received:', data); } return <Child onUpdate={handleUpdate} />; }适用场景:兄弟组件共享状态
实现原理:
React示例:
function Parent() { const [sharedData, setSharedData] = useState(''); return ( <> <BrotherA setData={setSharedData} /> <BrotherB data={sharedData} /> </> ); } function BrotherA({ setData }) { return <input onChange={(e) => setData(e.target.value)} />; } function BrotherB({ data }) { return <div>Received: {data}</div>; }适用场景:任意组件间通信(Vue2/Vue3需借助mitt库)
实现原理:
$emit/$on发布和订阅事件Vue3示例(使用mitt):
// eventBus.jsimportmittfrom'mitt';exportconstemitter=mitt();// 组件A(发送方)import{emitter}from'./eventBus';emitter.emit('message','Hello from ComponentA');// 组件B(接收方)import{emitter}from'./eventBus';import{onMounted,onBeforeUnmount}from'vue';exportdefault{setup(){consthandleMessage=(data)=>{console.log('Received:',data);};onMounted(()=>emitter.on('message',handleMessage));onBeforeUnmount(()=>emitter.off('message',handleMessage));}};适用场景:祖先组件向后代组件传递数据
实现原理:
provide提供数据inject接收数据Vue示例:
<!-- 祖先组件 --> <script setup> import { provide, ref } from 'vue'; const theme = ref('dark'); provide('theme', theme); </script> <!-- 后代组件 --> <script setup> import { inject } from 'vue'; const theme = inject('theme'); </script>适用场景:跨层级组件共享状态
实现原理:
Provider提供数据useContext获取数据React示例:
import { createContext, useContext, useState } from 'react'; const ThemeContext = createContext(); function App() { const [theme, setTheme] = useState('dark'); return ( <ThemeContext.Provider value={{ theme, setTheme }}> <DeepNestedComponent /> </ThemeContext.Provider> ); } function DeepNestedComponent() { const { theme } = useContext(ThemeContext); return <div>Current theme: {theme}</div>; }适用场景:中大型应用的全局状态管理
实现原理:
Pinia示例(Vue3):
// stores/counter.jsimport{defineStore}from'pinia';exportconstuseCounterStore=defineStore('counter',{state:()=>({count:0}),actions:{increment(){this.count++;}}});// 组件中使用import{useCounterStore}from'./stores/counter';constcounterStore=useCounterStore();适用场景:需要跨组件共享的响应式数据
实现原理:
Vue示例:
// sharedState.jsimport{reactive}from'vue';exportconststate=reactive({value:'',setValue(newVal){this.value=newVal;}});// 组件Aimport{state}from'./sharedState';state.setValue('Updated from ComponentA');// 组件Bimport{state}from'./sharedState';console.log(state.value);// 输出更新后的值| 通信方式 | 适用场景 | 优点 | 缺点 |
|---|---|---|---|
| Props | 父子组件数据传递 | 简单直观,类型安全 | 仅适用于父子组件 |
| 自定义事件 | 子组件向父组件通信 | 符合单向数据流原则 | 需要额外的事件处理逻辑 |
| 状态提升 | 兄弟组件共享状态 | 实现简单,易于理解 | 可能导致父组件过度复杂 |
| 事件总线 | 任意组件间通信 | 解耦组件,灵活性强 | 需要手动管理事件监听 |
| Provide/Inject | 跨层级组件通信 | 避免prop drilling | 数据追踪困难,维护成本高 |
| Context API | React跨层级状态共享 | 官方推荐,类型安全 | 需要额外包裹Provider组件 |
| 全局状态管理 | 中大型应用全局状态 | 集中管理,功能强大 | 学习成本高,配置复杂 |
| 共享响应式对象 | Vue3跨组件共享响应式数据 | 实现简单,响应式特性 | 需要手动管理数据同步 |
通过合理选择通信方式,开发者可以构建出结构清晰、易于维护的组件体系,显著提升开发效率和代码质量。