Vue3优雅的实现跨组件通信的常用方法总结

 更新时间:2023年11月13日 09:15:50   作者:萌萌哒草头将军  
开发中经常会遇到跨组件通信的场景,props 逐层传递的方法实在是太不优雅了,所以今天总结下可以更加简单的跨组件通信的一些方法,文中通过代码实例讲解的非常详细,需要的朋友可以参考下

前言

开发中经常会遇到跨组件通信的场景。props 逐层传递的方法实在是太不优雅了,所以今天总结下可以更加简单的跨组件通信的一些方法。

依赖注入

<!-- App.vue -->
<script setup lang="ts">
import { ref, provide } from "vue";
import Child from "./components/Child.vue";
const count = ref(0)
const updateCount = () => count.value ++
provide("count", {count, updateCount})
</script>

<template>
  <h4>公众号:萌萌哒草头将军</h4>
  <div>{{ count }}</div>
  <button @click="updateCount">change</button>
  <Child />
</template>
<!-- Child.vue -->
<template>
  <Other />
</template>
  
<script setup lang='ts'>
import Other from "./other.vue"
</script>

在 setup 组件中,使用 inject 跨组件通信是最佳的方案。所以该模式下,是没有提供event bus 事件总线。

但是在 option api 模式下,还需要额外的注册,显的有点麻烦。

<script lang='ts'>
export default {
  emits: ["some-name"]
}
</script>

属性透传

<!-- App.vue -->
<script setup lang="ts">
import { ref, provide } from "vue";
import Attr from "./components/Attr.vue";
const count = ref(0)
const updateCount = () => count.value ++
provide("count", {count, updateCount})
</script>

<template>
  <h4>公众号:萌萌哒草头将军</h4>
  <div>{{ count }}</div>
  <button @click="updateCount">change</button>
  <Attr :count="count" :updateCount="updateCount" />
</template>
<!-- Attr.vue -->
<template>
  <div>attr component</div>
  <Child v-bind="$attrs" />
</template>
  
<script setup lang='ts'>
import Child from './Child.vue';
</script>

属性透传这种方式类似于react中手动透传属性。感觉有点暴力,但是又特别方便快捷。

function App (props) {
  return <Other {...props} />
}

Vue中默认透传的属性有 style、class、key。如果子组件也存在class、style,则会自动合并class、style。

如果你的子组件是根组件时,可以省略 v-bind="$attrs"。

<template>
  <Child />
</template>

状态库

状态管理库我们以Pinia为例。

<!-- App.vue -->
<script setup lang="ts">
import Other from "./components/Other.vue";
import { useCounterStore } from "./store/index"
const state = useCounterStore()
</script>

<template>
  <h4>公众号:萌萌哒草头将军</h4>
  <div>{{ count }}</div>
  <button @click="updateCount">change</button>
  <Other />
</template>
import { defineStore } from "pinia" 
import { ref } from "vue"

export const useCounterStore = defineStore('counter', () => {
  const count = ref(0)
  function updateCount() {
    count.value++
  }

  return { count, updateCount }
})
<!-- Other.vue -->
<template>
  <div>pinia store</div>
  <div>{{ state.count }}</div>
  <button @click="state.updateCount">other change</button>
</template>
  
<script setup lang='ts'>
import { useCounterStore } from '../store';
const state = useCounterStore()
</script>

状态管理库最大的缺点是,没法使用解构语法。因为这会导致失去响应式的能力。

事件总线

事件总线(event bus)比较特殊,因为在组合式API里不支持该方式,所以下面的例子适合 Option API 组件。

<!-- App.vue -->
<script setup lang="ts">
import { ref } from "vue";
import Other from "./components/Other.vue";
const count = ref(0)
const updateCount = () => count.value ++
</script>

<template>
  <h4>公众号:萌萌哒草头将军</h4>
  <div>{{ count }}</div>
  <button @click="updateCount">change</button>
  <Other @updateCount="updateCount()" />
</template>
<!-- Other.vue -->
<template>
  <div>eventBus store</div>
  <button @click="$emit('updateCount')">other change</button>
</template>
  
<script lang='ts'>
export default {
  emits: ["updateCount"]
}
</script>

事件总线更适合传递事件。

自定义事件

但是有时候,你可能非常想使用事件总线的方式在 setup 组件中传递事件,这时候我们可以使用自定义的事件的方式实现这种功能。

下面是实现。

class EventBus {
  constructor() {
    this.events = {};
  }

  // 订阅事件
  on(eventName, callback) {
    if (!this.events[eventName]) {
      this.events[eventName] = [];
    }
    this.events[eventName].push(callback);
  }

  // 发布事件
  emit(eventName, eventData) {
    const eventCallbacks = this.events[eventName];
    if (eventCallbacks) {
      eventCallbacks.forEach(callback => {
        callback(eventData);
      });
    }
  }

  // 取消订阅事件
  off(eventName, callback) {
    const eventCallbacks = this.events[eventName];
    if (eventCallbacks) {
      this.events[eventName] = eventCallbacks.filter(cb => cb !== callback);
    }
  }
}

export const eventBus = new EventBus()
<!-- App.vue -->
<script setup lang="ts">
import { ref } from "vue";
import Other from "./components/Other.vue";
import { eventBus } from "./store/eventBus";
const count = ref(0)
const updateCount = () => count.value ++
eventBus.on("updateCount", updateCount)
</script>

<template>
  <h4>公众号:萌萌哒草头将军</h4>
  <div>{{ count }}</div>
  <button @click="updateCount">change</button>
  <Other @updateCount="updateCount()" />
</template>
<!-- Other.vue -->
<template>
  <div>eventBus</div>
  <button @click="eventBus.emit('updateCount', null)">other change</button>
</template>
  
<script setup lang='ts'>
import { eventBus } from "../store/eventBus";
</script>

当然,我们这里不止可以使用 event bus,发布订阅模式也很适合。可以参考我以前的设计模式的文章实现这个功能。

总结

每种方式都有自己的优点和缺点,根据使用场景选择最合适的才能算是最优的方案。

以上就是Vue3优雅的跨组件通信的常用方法总结的详细内容,更多关于Vue3跨组件通信的资料请关注脚本之家其它相关文章!

相关文章

  • vue的@change的用法及操作代码

    vue的@change的用法及操作代码

    @change 是 Vue.js 中用于监听表单元素值变化的事件处理器,这篇文章主要介绍了vue的@change的用法,需要的朋友可以参考下
    2023-10-10
  • 解决vue单页面修改样式无法覆盖问题

    解决vue单页面修改样式无法覆盖问题

    这篇文章主要介绍了vue单页面修改样式无法覆盖问题,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • 关于vite+vue3打包部署问题

    关于vite+vue3打包部署问题

    这篇文章主要介绍了关于vite+vue3打包部署问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • vue3 Class 与 Style 绑定操作方法

    vue3 Class 与 Style 绑定操作方法

    数据绑定的一个常见需求场景是操纵元素的 CSS class 列表和内联样式,因为 class 和 style 都是 attribute,我们可以和其他 attribute 一样使用 v-bind 将它们和动态的字符串绑定,这篇文章主要介绍了vue3 Class 与 Style 绑定操作方法,需要的朋友可以参考下
    2024-05-05
  • vue指令中的v-once用法

    vue指令中的v-once用法

    这篇文章主要介绍了vue指令中的v-once用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • Vue组件实现旋转木马动画

    Vue组件实现旋转木马动画

    这篇文章主要为大家详细介绍了Vue组件实现旋转木马动画效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • vue导出excel多层表头的实现方案详解

    vue导出excel多层表头的实现方案详解

    这篇文章主要为大家详细介绍了vue导出excel多层表头的实现方案,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以跟随小编一起学习一下
    2025-04-04
  • vue导出excel无法打开问题及解决

    vue导出excel无法打开问题及解决

    在Vue项目中导出Excel文件时,需在请求中设置responseType为'blob'或'arraybuffer',否则文件可能损坏无法打开,正确配置确保浏览器正确解析二进制数据流,生成有效Excel文件
    2025-08-08
  • vue 组件中使用 transition 和 transition-group实现过渡动画

    vue 组件中使用 transition 和 transition-group实现过渡动画

    本文给大家分享一下vue 组件中使用 transition 和 transition-group 设置过渡动画,总结来说可分为分为 name 版, js 钩子操作类名版, js 钩子操作行内样式版,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友参考下吧
    2019-07-07
  • Vue使用Proxy代理后仍无法生效的解决

    Vue使用Proxy代理后仍无法生效的解决

    这篇文章主要介绍了Vue使用Proxy代理后仍无法生效的解决,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11

最新评论