当前位置: 首页 > news >正文

常用的几种 Vue 父子组件传值方式

1. 父组件向子组件传值(props)

  • 父组件代码:Parent.vue

    <template><div><h2>父组件</h2><Child :parent-msg="parentMsg" /></div>
    </template><script>
    import Child from './Child.vue';export default {components: {Child},data() {return {parentMsg: '这是父组件传递的消息'};}
    };
    </script>
  • 子组件代码:Child.vue

    <template><div><h3>子组件</h3><p>从父组件接收到的消息:{{ parentMsg }}</p></div>
    </template><script>
    export default {props: {parentMsg: {type: String,required: true}}
    };
    </script>

    2. 子组件向父组件传值(this.$emit)

  • 子组件代码:Child.vue

    <template><div><h3>子组件</h3><button @click="sendMessage">向父组件发送消息</button></div>
    </template><script>
    export default {data() {return {childMsg: '这是子组件的消息'};},methods: {sendMessage() {this.$emit('sendMessage', this.childMsg);}}
    };
    <
http://www.dtcms.com/a/144196.html

相关文章:

  • 使用 GitHub Actions 和 Nuitka 实现 Python 应用(customtkinter ui库)的自动化跨平台打包
  • 状态管理最佳实践:Bloc架构实践
  • Android Jetpack Compose 状态管理解析:remember vs mutableStateOf,有啥不一样?为啥要一起用?
  • HTML表单与数据验证设计
  • 区块链预言机(Oracle)详解:如何打通链上与现实世界的关键桥梁?
  • 如何将自己封装的组件发布到npm上:详细教程
  • JavaScript学习教程,从入门到精通,DOM节点操作语法知识点及案例详解(21)
  • Android学习总结之APK打包流程
  • 使用Ingress发布应用程序
  • swift-12-Error处理、关联类型、assert、泛型_
  • ospf实验
  • 【HDFS入门】HDFS性能调优实战:压缩与编码技术深度解析
  • JavaScript中的Event事件对象详解
  • STL之vector基本操作
  • c语言中的原,反,补码
  • `Accelerate`库实现模型并行计算
  • STM32单片机入门学习——第42节: [12-2] BKP备份寄存器RTC实时时钟
  • QML动画--ParallelAnimation和SequentialAnimation
  • linux查看目录相关命令
  • SpringBoot启动后初始化的几种方式
  • 《关于加快推进虚拟电厂发展的指导意见》解读
  • Windows进程管理
  • 微信、抖音、小红书emoji符号大全
  • 【工具变量】A股上市公司信息披露质量KV指数测算数据集(含do代码 1991-2024年)
  • 多线程使用——线程安全、线程同步
  • -SSRF 服务端请求Gopher 伪协议无回显利用黑白盒挖掘业务功能点
  • Scade 语言词法介绍
  • 4.17--4.19刷题记录(贪心)
  • RTMP握手流程
  • 完整游戏排行榜系统实现