子组件向父组件传值$emit

点击子组件的按钮,将子组件的值传递给父组件,并进行提示。

子组件

javascript 复制代码
<template>
  <div>
    <button @click="emitIndex">clickme</button>
  </div>
</template>
<script>
export default {
  methods: {
    emitIndex() {
      //$emit 绑定一个自定义事件, 当这个语句被执行时, 就会将参数 arg 传递给父组件,父组件通过 v-on 监听并接收参数。
      //returnData:对应父组件的函数名
      //hello world:传递给returnData函数的值
      this.$emit('returnData', 'hello world')
    }
  }
}
</script>

父组件

javascript 复制代码
 
 <template>
  <!-- 子组件中$emit绑定的returnData事件 -->
  <childemit @returnData="returnChildData"></childemit>
</template>
<script>
// 引用子组件
import Childemit from './testComponent/childemit.vue'
export default {
  methods: {
    //returnChildData:绑定的函数
    //childStr:子组件传过来的值
    returnChildData(childStr) {
      alert('这里是父组件,接收到子组件的值为:' + childStr)
    }
  },
  // 组件注册
  components: {
    Childemit
  }
}
</script>

效果

相关推荐
_xaboy12 小时前
低代码可视化表单设计器 FcDesigner 远程请求教程:GET POST 变量鉴权
低代码·开源·vue·表单·fcdesigner
宠友信息4 天前
IM系统开发技术路线分析,即时通讯源码助力快速搭建聊天应用
java·spring boot·redis·websocket·mysql·uni-app·vue
南城以南溫暖如初1475 天前
从传统健身房到无人值守:24小时自助健身系统的技术实现路径
java·spring boot·mysql·架构·vue
新中地GIS开发老师5 天前
WebGIS开发学生作品 | 基于无人机影像的烟草检测智能体与数字化管理平台
vue·web·mapbox·webgis
南城以南溫暖如初1475 天前
外卖CPS实战指南:从技术选型到运营落地全流程解析
java·spring boot·mysql·架构·vue
aXin_ya6 天前
ElementUI (01):Vue2 项目引入 ElementUI使用
elementui·vue
常宇佳7 天前
vue3 @代指src路径设置
前端·typescript·vue
北城笑笑7 天前
Server 18 ,Nginx + Vite 前端部署排错实战:从 `/api/gpt/chat` 404 到 9012 后端服务的完整定位过程
linux·运维·前端·nginx·ubuntu·vue
小强库计算机毕业设计10 天前
SpringBoot+Vue3 学生宿舍管理系统实战
java·spring boot·后端·vue·学生宿舍管理系统
TELL52113 天前
vue前端架构
vue