Vue组件:使用$emit()方法监听子组件事件

1、监听自定义事件

父组件通过使用 Prop 为子组件传递数据,但如果子组件要把数据传递回去,就需要使用自定义事件来实现。父组件可以通过 v-on 指令(简写形式"@")监听子组件实例的自定义事件,而子组件可以通过调用内建的 $emit() 方法并传入事件名称来触发自定义事件。

组件之间的数据传递:

父传子:使用 Props 属性。

子传父:使用 $emit() 方法。

$emit() 方法的语法格式如下:

javascript 复制代码
this.$emit( eventName, [...args] )

参数说明:

eventName:传入事件的名称。

...args:触发事件传递的参数,该参数是非必选。

**【实例】**使用$emit()方法,实现子组件向父组件传递事件。

(1)创建 ParentComponent.vue 父组件

html 复制代码
<template>
    <fieldset>
        <legend>父组件</legend>
        <h3>父组件接收到子组件传递的数据:</h3>
        <p>博客信息:{{ blogName }}</p>
        <p>博客信息:{{ blogUrl }}</p>

        <!-- 第三步:使用组件 -->
        <ChildComponent @receiverData="getBlogInfo" />
    </fieldset>
</template>

<script>
//第一步:引用组件
import ChildComponent from '@/components/ChildComponent.vue'

export default {
    data() {
        return {
            blogName: '',
            blogUrl: ''
        }
    },
    //第二步:注册组件
    components: {
        ChildComponent,
    },
    //方法
    methods: {
        getBlogInfo: function (blogName, blogUrl) {
            this.blogName = blogName;
            this.blogUrl = blogUrl;
        }
    }
}
</script>

(2)创建 ChildComponent.vue 子组件

html 复制代码
<template>
    <fieldset>
        <legend>子组件</legend>
        <button @click="sendData">传递数据给父组件</button>
    </fieldset>
</template>

<script>
export default {
    data() {
        return {
            blogName: '您好,欢迎访问 pan_junbiao的博客',
            blogUrl: 'https://blog.csdn.net/pan_junbiao'
        }
    },
    methods: {
        sendData: function () {
            // 核心代码:使用 $emit() 方法
            this.$emit('receiverData', this.blogName, this.blogUrl);
        }
    }
}
</script>

(3)在 App.vue 根组件中,引入父组件

html 复制代码
<template>
  <!-- 第三步:使用组件 -->
  <ParentComponent />
</template>

<script>
//第一步:引用组件
import ParentComponent from '@/components/ParentComponent.vue'

export default {
  //第二步:注册组件
  components: {
    ParentComponent,
  }
}
</script>

<style></style>

执行结果:

2、组件事件配合 v-model 指令

如果是在子组件中用户输入数据,我们希望在获取数据的同时发生数据给父组件,这是可以配合 v-model 指令使用。

**【实例】**子组件中用户输入数据,在父组件中实时获取数据。

(1)修改 ParentComponent.vue 父组件

html 复制代码
<template>
    <fieldset>
        <legend>父组件</legend>

        <!-- 第三步:使用组件 -->
        <ChildComponent @searchEvent="getSearch" />

        <h3>父组件接收到子组件传递的数据:</h3>
        接收到的搜索关键字:<input type="text" v-model="search" />
    </fieldset>
</template>

<script>
//第一步:引用组件
import ChildComponent from '@/components/ChildComponent.vue'

export default {
    data() {
        return {
            search: ''
        }
    },
    //第二步:注册组件
    components: {
        ChildComponent,
    },
    //方法
    methods: {
        getSearch: function (keyword) {
            this.search = keyword;
        }
    }
}
</script>

<style>
input {
    width: 300px;
    padding: 3px;
    font-size: 16px;
}
</style>

(2)修改 ChildComponent.vue 子组件

html 复制代码
<template>
    <fieldset>
        <legend>子组件</legend>
        搜索:<input type="text" v-model="search" />
    </fieldset>
</template>

<script>
export default {
    data() {
        return {
            search: ''
        }
    },
    // 监听器
    watch: {
        search(newValue, oldValue) {
            // 核心代码:使用 $emit() 方法
            this.$emit('searchEvent', newValue);
        }
    }
}
</script>

执行结果:

相关推荐
夜焱辰15 小时前
浏览器端 Agent 的文件版本管理:不用 Git,基于 OPFS + SQLite 自己造了一个
前端·人工智能
梦想的颜色15 小时前
TypeScript 完全指南(下):从类型体操到生产级配置
前端·javascript·typescript
Hi~晴天大圣16 小时前
npm使用介绍
前端·npm·node.js
888CC++17 小时前
如何在 C 语言中进行程序调试?
前端·javascript·算法
喵个咪17 小时前
基于 Taro 的 Headless CMS 多端前端架构:技术解析与二次开发导引
前端·react.js·taro
狂炫冰美式17 小时前
你还在古法PPT吗,试试HTML呢?免费编辑导出工具给 xdm 放这了
前端·后端·github
万少18 小时前
未来组织的分水岭不是员工数量,而是人才密度
前端·后端·面试
任磊abc18 小时前
nextjs16配置eslint+prettier
前端·eslint·nextjs·prettier
x***r15118 小时前
Another-Redis-Desktop-Manager.1.3.7安装步骤详解(附Redis可视化连接与Key管理教程)
前端·bootstrap·html
Captaincc18 小时前
你真的知道自己把 AI 用在了哪里吗?这是 Vibe Usage 想回答的问题
前端·vibecoding