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

效果

相关推荐
名字很费劲6 小时前
vue项目,刷新后出现404错误,怎么解决
前端·javascript·vue·404
码界筑梦坊7 小时前
329-基于Python的交通流量数据可视化分析系统
开发语言·python·信息可视化·数据分析·django·vue·毕业设计
Thomas.Sir12 小时前
第五章:RAG知识库开发之【利用RAG知识库实现智能AI系统:从零构建企业级智能问答应用】
人工智能·python·vue·状态模式·fastapi·智能
Rysxt_13 小时前
Vue 3.4+ 实验性/新特性深度实战(2026版)
vue
码界筑梦坊1 天前
353-基于Python的大湾区气候数据可视化分析系统
开发语言·python·信息可视化·数据分析·django·vue·毕业设计
澄江静如练_1 天前
ref和reactive
vue
码界筑梦坊2 天前
336-基于Python的肺癌数据可视化分析预测系统
开发语言·python·信息可视化·数据分析·django·vue·毕业设计
Thomas.Sir3 天前
第十四章:基于 FastAPI+Vue3 的智能聊天系统全栈开发实战
vue·状态模式·fastapi·智能
abigale033 天前
从零实现 AI 聊天助手:可直接复用的前端核心方案
chatgpt·vue·流式输出
前端飞行手册5 天前
electron应用开发模板,集成多种解决方案
前端·javascript·学习·electron·前端框架·vue