VUE中使用this.$emit()的使用进行父子组件之间传值

vue中使用this.$emit()的使用:

子组件child.vue

<template>
    <button @click=buttonClick>点击事件</button>
</template>

export default {
    name: 'ChildComponent',
    methods: {
        buttonClick() {
         // 触发点击事件,并传递数据给父组件
           this.$emit('parent-event','xiaoming')
        }

    }
}

当点击时,进入到buttonClick()方法,此时调用this.$emit()方法触发自定义事件parent-event

并传递了'xiaoming'作为数据

父组件parent.vue通过监听parent-event来接收这个自定义事件并执行

<template>
    <child @parent-event="handleParentEvent"></child>
</template>

// 在父组件中引入子组件
import childComponent  from '@/component/childComponent'

export default {
    name: 'Parent',
    data(){
        return {
        }
    },
// 注册子组件
   components: {
        childComponent
     },
   methods: {
        handleParentEvent(data) {
            // 处理自定义事件的逻辑
               console.log(data)// 输出'xiaoming'
        }
    }
}

参考文章:Vue中的this.$emit()方法详解_vue.js_脚本之家 (jb51.net)

相关推荐

  1. VUE使用this.$emit()使用进行父子组件之间

    2024-07-16 13:18:04       22 阅读
  2. Vue2之父子组件使用watch监听props对象

    2024-07-16 13:18:04       28 阅读
  3. vue3+vite+ts父子组件之间

    2024-07-16 13:18:04       68 阅读
  4. vue3父子组件之间方式

    2024-07-16 13:18:04       43 阅读
  5. vue父子组件

    2024-07-16 13:18:04       55 阅读
  6. Vue3父子组件问题

    2024-07-16 13:18:04       28 阅读
  7. vue 父子组件之间通过 v-model

    2024-07-16 13:18:04       57 阅读
  8. vue3使用mitt用于组件之间

    2024-07-16 13:18:04       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 13:18:04       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 13:18:04       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 13:18:04       57 阅读
  4. Python语言-面向对象

    2024-07-16 13:18:04       68 阅读

热门阅读

  1. Python-数据爬取(爬虫)简介

    2024-07-16 13:18:04       23 阅读
  2. 讲解机器学习中的 K-均值聚类算法及其优缺点

    2024-07-16 13:18:04       24 阅读
  3. c++单例模式

    2024-07-16 13:18:04       29 阅读
  4. ArrayList详解

    2024-07-16 13:18:04       22 阅读
  5. 系统架构设计师知识点总结目录篇

    2024-07-16 13:18:04       21 阅读
  6. StudentRequest

    2024-07-16 13:18:04       16 阅读
  7. Mysql知识大全

    2024-07-16 13:18:04       15 阅读
  8. 系统架构师(每日一练)

    2024-07-16 13:18:04       20 阅读