子组件向父组件传值$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>

效果

相关推荐
Shi_haoliu18 小时前
SolidTime 在 Rocky Linux 9.5 上的完整部署流程
linux·运维·nginx·postgresql·vue·php·laravel
Mast Sail1 天前
WebStrom+Vitesse+Vue3项目路径报错爆红问题
vue·vite·webstorm
全栈前端老曹1 天前
【前端】Hammer.js 快速上手入门教程
开发语言·前端·javascript·vue·react·移动端开发·hammer.js
response_L1 天前
PageOffice动态给word插入二维码(或条形码)图片
vue·word·开发工具·二维码·在线编辑
华玥作者2 天前
uni-app + Vite 项目中使用 @uni-helper/vite-plugin-uni-pages 实现自动路由配置(超详细)
前端·uni-app·vue·vue3·vite
狼性书生2 天前
uniapp+vue3实现的简单吐司通知弹窗组件
前端·uni-app·vue·组件·插件
ghostmen2 天前
SpringBoot + Vue 实现 Python 在线调试器 - 技术方案文档
java·python·vue·springboot
qiqiliuwu2 天前
VUE3+TS+ElementUI项目中监测页面滚动scroll事件以及滚动高度不生效问题的解决方案(window.addEventListener)
前端·javascript·elementui·typescript·vue
喔烨鸭2 天前
antdv编辑表格,根据选择时间区间展示动态列
前端·vue·表格编辑
一只小阿乐3 天前
vue-web端网站 滑动进行分页
前端·javascript·vue.js·vue·分页