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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
LINUX DO - 热门话题
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threat Research - Cisco Blogs
AI
AI
B
Blog RSS Feed
S
Schneier on Security
雷峰网
雷峰网
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Cloudbric
Cloudbric
L
LINUX DO - 最新话题
罗磊的独立博客
有赞技术团队
有赞技术团队
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
The Hacker News
The Hacker News
博客园 - Franky
Attack and Defense Labs
Attack and Defense Labs
The Cloudflare Blog
Webroot Blog
Webroot Blog
Last Week in AI
Last Week in AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 叶小钗
美团技术团队
L
Lohrmann on Cybersecurity
T
The Blog of Author Tim Ferriss
The Last Watchdog
The Last Watchdog
T
Troy Hunt's Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
Know Your Adversary
Know Your Adversary
O
OpenAI News
博客园 - 【当耐特】
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cybersecurity and Infrastructure Security Agency CISA
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
PCI Perspectives
PCI Perspectives
H
Heimdal Security Blog
I
InfoQ
GbyAI
GbyAI
T
Threatpost
C
Cisco Blogs

博客园 - 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使用构建工具时,关于本地图片的引用 如何改变type="checkbox"的input复选框的样式 vue3中非父子组件、非祖先-后代组件之间传递方法 解决 IntelliJ IDEA 中项目右键没有 Git 菜单的问题 关于 ref 为 DOM 元素或子组件实例动态创建引用的总结 Git 合并时出现 Please enter a commit message to explain why this merge is necessary 问题解决 拉取远程其他分支的最新代码
vue3 中父组件调用子组件的方法,子组件是不是必须暴露方法
smil、梵音 · 2025-03-31 · via 博客园 - smil、梵音

在 Vue 3 中,父组件调用子组件的方法时,子组件 必须暴露方法,否则父组件无法直接访问子组件的内部方法。

原因

Vue 3 的设计原则是强调组件的封装性和可维护性。子组件的方法默认是私有的,只有通过明确暴露(exposedefineExpose)才能被父组件访问。这种设计有助于避免直接访问子组件内部实现的潜在问题,比如破坏封装性或导致难以维护的代码。

如何暴露方法

在 Vue 3 中,可以通过以下方式暴露子组件的方法:

1. Composition API

在 Composition API 中,使用 defineExpose 来暴露方法:

<script setup>
import { ref } from 'vue'

const count = ref(0)

function increment() {
  count.value++
}

// 暴露方法
defineExpose({
  increment
})
</script>

2. Options API

在 Options API 中,使用 expose 选项来暴露方法:

export default {
  data() {
    return {
      count: 0
    }
  },
  methods: {
    increment() {
      this.count++
    }
  },
  expose: ['increment'] // 暴露方法
}

父组件调用子组件方法

父组件可以通过 refprovide/inject 来调用子组件暴露的方法。

示例:通过 ref

<template>
  <div>
    <ChildComponent ref="childRef" />
    <button @click="callChildMethod">调用子组件方法</button>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import ChildComponent from './ChildComponent.vue'

const childRef = ref(null)

function callChildMethod() {
  childRef.value.increment() // 调用子组件暴露的方法
}
</script>

总结

  • 子组件必须通过 defineExpose(Composition API)或 expose(Options API)来暴露方法。

  • 父组件通过 ref 或其他机制访问子组件暴露的方法。

  • 这种设计有助于保持组件的封装性和代码的可维护性。