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

推荐订阅源

C
Check Point Blog
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
美团技术团队
NISL@THU
NISL@THU
C
Cisco Blogs
SecWiki News
SecWiki News
N
Netflix TechBlog - Medium
Forbes - Security
Forbes - Security
Cloudbric
Cloudbric
雷峰网
雷峰网
T
Tailwind CSS Blog
博客园 - 司徒正美
The Register - Security
The Register - Security
L
LangChain Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Threat Research - Cisco Blogs
I
InfoQ
S
Schneier on Security
L
Lohrmann on Cybersecurity
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
Schneier on Security
Schneier on Security
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
K
Kaspersky official blog
Google DeepMind News
Google DeepMind News
Cisco Talos Blog
Cisco Talos Blog
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
D
Docker
N
News | PayPal Newsroom
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Hacker News: Front Page
云风的 BLOG
云风的 BLOG
Microsoft Security Blog
Microsoft Security Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 聂微东
Webroot Blog
Webroot Blog
MongoDB | Blog
MongoDB | Blog

博客园 - smil、梵音

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的使用 IDEA中如何实现git的cherry-pick可视化操作? Windows11添加系统环境变量 VS Code——Live Server的简介、安装与使用 vue3中 pinia 的运用 vscode使用region折叠任意想折叠的代码块 我本地分支代码推送到远程的另一个分支,怎么操作? idea中打开多文件编辑器换行展示 本地代码强制覆盖远程代码,本地代码强制和远程某一个分支保持一致,再覆盖远程代码 css头部固定,内容区域高度自适应剩余的屏幕高度,内容区域过多显示竖向滚动条 flex为1的元素宽度自适应的同时,文本太长的时候显示省略号 idea中项目文件目录消失如何解决 vue3插入图片 vue3使用构建工具时,关于本地图片的引用 vue3 中父组件调用子组件的方法,子组件是不是必须暴露方法 如何改变type="checkbox"的input复选框的样式 解决 IntelliJ IDEA 中项目右键没有 Git 菜单的问题 关于 ref 为 DOM 元素或子组件实例动态创建引用的总结 Git 合并时出现 Please enter a commit message to explain why this merge is necessary 问题解决 拉取远程其他分支的最新代码
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 提供了一种集中式的状态管理方式,使得组件间的通信变得更加简单和高效。