惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
宝玉的分享
宝玉的分享
G
Google Developers Blog
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
V
Visual Studio Blog
博客园 - Franky
S
SegmentFault 最新的问题
Jina AI
Jina AI
爱范儿
爱范儿
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
C
Check Point Blog
月光博客
月光博客
P
Proofpoint News Feed
T
The Blog of Author Tim Ferriss
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
Martin Fowler
Martin Fowler

博客园 - smil、梵音

css渐变边框的实现 C primer Plus 第六版第4章节复习题5记录 C语言第4章末尾程序找错记录 css 的z-index的层级问题 由webai项目的 AI工作台 项目有感记录 C语言标准(C89/C90)中的32个关键字全列表 Windows 系统的 vscode 通过 Remote-SSH 连接虚拟机的 Ubuntu 系统运行 C 语言的代码 Windows 系统 vscode Remote-SSH 远程连接虚拟机里面的Ubuntu系统的失败处理 Ubuntu虚拟机里安装了SSH,开启SSH服务的时候报错 VS Code的Remote - SSH功能 Ubuntu系统里面安装 g++ ,sudo apt update 的含义 C语言是不是必须得通过gcc编译成可执行的程序? c语言用gcc编译过后,执行 ./hello.c 报错 ./hello.c: 权限不够 windows系统的虚拟机里面的Ubuntu系统安装VMware Tools Ubuntu的应用中心搜索 vscode 的时候,筛选条件为Snap包、debian包是什么意思,如何安装vscode Ubuntu系统是否就是Linux系统,安装软件和Windows系统有何区别 Ubuntu系统里面vscode运行C语言的第一个 Hello World 程序 vscode 中安装C/C++相关的插件 Ubuntu 安装一个轻量级的中文输入法Fcitx5 Ubuntu安装百度网盘 Ubuntu系统里面安装vscode 乌版图系统截屏快捷键 Ubuntu 系统里面运行C++ TortoiseGit 的安装与汉化 安装虚拟机+ ubuntu24.04 系统 联想小新安装 ubuntu24.04 双系统 | 安装乌版图系统 | windows安装 ubuntu24.04 双系统 利用faststone capture制作gif动图 Sublime Text 常用编辑快捷键速查表 vscode 中的 css 样式代码不显示折叠图标的解决方法 IDEA中git的Cherry-Pick的可视化使用(个人总结) IDEA中git的Cherry-Pick的使用
vue3中非父子组件、非祖先-后代组件之间传递方法
smil、梵音 · 2025-03-24 · via 博客园 - smil、梵音

在 Vue 3 中,当需要在非父子组件或非祖先-后代组件之间传递方法,并且要求一个组件发出消息后另一个组件能够实时接收时,可以使用 Pinia 状态管理器来实现。Pinia 是 Vue 3 的官方状态管理库,它允许你在不同的组件间共享状态,并且可以实时响应状态的变化。

以下是使用 Pinia 来实现组件间通信的基本步骤:

1、创建 Pinia Store

首先,你需要创建一个 Pinia store 来存储状态和定义修改状态的方法。在 `store` 目录下创建一个新的文件,例如 `useMessageStore.js`,并定义你的 store。

// useMessageStore.js
  import { defineStore } from 'pinia';

  export const useMessageStore = defineStore('message', {
    state: () => ({
      message: ''
    }),
    actions: {
      setMessage(msg) {
        this.message = msg;
      }
    }
  });

2、在组件中使用 Store

然后,在需要发送和接收消息的组件中使用这个 store。你可以使用 `useStore` 函数来获取 store 的实例,并访问其状态和方法。

发送消息的 .vue 组件

  <template>
    <button @click="sendMessage">发送消息</button>
  </template>

  <script setup>
  import { useMessageStore } from '@/stores/useMessageStore';

  const messageStore = useMessageStore();

  function sendMessage() {
    messageStore.setMessage('Hello from Component A');
  }
  </script>

接收消息的 .vue 组件

  <template>
    <div>{{ message }}</div>
  </template>

  <script setup>
  import { useMessageStore } from '@/stores/useMessageStore';
  import { watch } from 'vue';

  const messageStore = useMessageStore();

  // 使用 watch 监听 message 的变化
  watch(() => messageStore.message, (newMessage) => {
    console.log('Received message:', newMessage);
  });
  </script>

3、实时响应状态变化

在上面的接收消息组件中,我们使用了 Vue 3 的 `watch` 函数来监听 `message` 状态的变化。当 `message` 状态被更新时,`watch` 函数会自动执行,从而实现实时接收消息的效果。

通过这种方式,你可以在 Vue 3 中实现非父子组件或非祖先-后代组件之间的通信,并且能够实时响应状态的变化。Pinia 提供了一种集中式的状态管理方式,使得组件间的通信变得更加简单和高效。