跳至主要內容
React 通讯方式

简述

在 React 中,组件通信是非常重要的,因为一个复杂的应用程序通常由多个组件组成。

Props

这是 React 中最基本的组件通信方式。通过将数据作为属性传递给子组件,可以实现从父组件向子组件传递数据。这是一种单向传递的方式,父组件向子组件传递数据,子组件只能读取这些数据。

function ParentComponent() {
    const data = "Hello from parent!";
    return <ChildComponent message={data} />;
}

// 子组件
function ChildComponent(props) {
    return <p>{props.message}</p>;
}

石怜安大约 1 分钟ReactFramework
Vue 通讯方式

简述

Vue是数据驱动视图更新的框架,所以对于vue来说组件间的数据通信非常重要。

常见使用场景可以分为三类:

  • 父子组件通信: props/$emit $parent/$children provide/inject ref $attrs/$listeners

  • 兄弟组件通信: eventBus Vuex

  • 跨级通信: eventBus Vuex provide/inject $attrs/$listeners


石怜安大约 5 分钟VUEFramework