Vue.js自定义事件的使用(实现父子之间的通信)

2023-05-16

vue

v-model修饰符:.lazy、.number、.trim

$attrs数据的透传,在组件(这个是写在App.vue中),数据就透传到student组件中,在template中可以直接使用{{$attrs.students}}获取数据

通过defineProps定义的属性在attrs中就不存在了,通过自定义属性时,最好通过defineProps来声明一下

app.vue中,组件中通过defineProps来进行自定义属性。推荐这种方式。

<script setup>
	const props = defineProps(["students"])
    //删除
    const delStuHandler = (index)=>{
        if(confirm("确认删除?")){
            props.students.splice(index,1)
        }
    }
</script>
<template>
<tr v-for="(stu,index) in props.students">
    <td>{{stu.id}}</td>
    <td>
    	<a href="#" @click.prevent="delStuHandler(index)"></a>
    </td>
</tr>
</template>

:表示普通属性,@表示事件的形式

自定义事件

上面的方式在模板中去修改了App.vue的属性(STU_ARR),这种方式不好,好的方式是“自己管理对象”的方式,因此可以使用自定义事件的方式实现

app.vue

以属性的方式将函数传进去,使用props进行接收

<script setup>
import Student from "./components/Student.vue"
import {ref} from "vue"
const STU_ARR = ref([
    {
        id:0,
        name:"1"
        age:24,
        gender:"男",
        address:"河南"
    },
        {
        id:0,
        name:"1"
        age:24,
        gender:"男",
        address:"河南"
    },
        {
        id:0,
        name:"1"
        age:24,
        gender:"男",
        address:"河南"
    }
])
const delStuByIndex = (index)=>{
    STU_ARR.value.splice(index,1)
}
</script>
<template>
<Student :student="STU_ARR" :fn="delStuByIndex"></Student>
</template>

在student.vue中

<script setup>
	const props = defineProps(["students","fn"])
    const delStuHandler = (index)=>{
        if(confirm("确认删除?")){
            // props.students.splice(index,1)
            props.fn(index)
        }
    }
</script>

使用事件的方式传递,@del-stu使用这个方式进行事件命名。

我们可以将组件中的方法(app.vue)以自定义事件的形式发送给其他的组件,此时不能通过defineProps接收了

<script setup>
import Student from "./components/Student.vue"
import {ref} from "vue"
const STU_ARR = ref([
    {
        id:0,
        name:"1"
        age:24,
        gender:"男",
        address:"河南"
    },
        {
        id:0,
        name:"1"
        age:24,
        gender:"男",
        address:"河南"
    },
        {
        id:0,
        name:"1"
        age:24,
        gender:"男",
        address:"河南"
    }
])
const delStuByIndex = (index)=>{
    STU_ARR.value.splice(index,1)
}
</script>
<template>
<Student :student="STU_ARR" @del-stu="delStuByIndex"></Student>
</template>

在student.vue中使用方式:

在模板中可以通过$emit()来触发自定义事件

事件定义时使用"-"的命名方式,在使用的时候可以使用驼峰的方式进行使用

<script setup>
	const props = defineProps(["students"])
    const emits = defineEmits(["delStu"])
    //删除
    const delStuHandler = (index)=>{
        if(confirm("确认删除?")){
           // props.students.splice(index,1)
           // props.fn(index)
            emits("delStu",index)
        }
    }
</script>
<template>
<tr v-for="(stu,index) in props.students">
    <td>{{stu.id}}</td>
    <td>
    	<!--
			<a href="#" @click.prevent="$emit('delStu',index)"></a>
        	<a href="#" @click.prevent="emits('delStu',index)"></a>
		-->
        <a href="#" @click.prevent="delStuHandler(index)"></a>
    </td>
</tr>
</template>

自定义事件的使用场景:

当我们需要调用其他组件上的方法,比如app.vue或者其他组件,我们可以通过自定义事件的方式将方法传给需要调用方法的组件,使用emit进行触发调用,很方便。

在一些子组件给父组件传信息的时候,因为props是自上向下传递数据的,父组件给子组件设置props,给子组件传递数据,因此可以使用自定义事件的方式实现父子之间的通信。(props是单向的,但是也是可以实现子传父的操作,需要在父组件中定义一个方法,在合适的时机,子组件触发这个函数,就可以实现子传父的操作,但是实现起来不如自定义事件方便)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Vue.js自定义事件的使用(实现父子之间的通信) 的相关文章

  • C语言细讲——结构体和简单链表

    本博文是该系列的最后一个内容 xff1a 结构体 作者 xff1a 小 琛 欢迎转载 xff0c 请标明出处 引言 xff1a 结构是一些值的集合 xff0c 这些值称为成员变量 xff0c 且这些变量可以是不同的类型 单结构体并没有多少内
  • 什么是juc

    juc是用于处理线程的工具包
  • 缓存数据一致性解决方案

    读写锁 xff0c 读相当于无锁状态 xff0c 写加上锁 读多写多 xff0c 直接去数据库查询 读多写少 xff0c 完全可以使用Spring Cache
  • 什么是nginx动静分离

  • 检索DSL是什么

    DSL是ElasticSearch的高级搜索
  • 线程池的工作顺序

  • sku和spu的区别

    spu和sku就是上下级关系 xff0c spu是上级 xff0c sku是下级 假如没有选择任何类型那么他就是一个单独的spu xff0c 但是当它选择了具体的颜色 xff0c 版本 xff0c 购买方式等等 xff0c 那么他就是一个s
  • hexo next 博客,jsdelivr cdn报错无法访问

    一 博客环境 我的hexo版本是5 4 0 xff0c next版本是7 8 0 因 jsdelivr cdn报错导致博客首页无法访问 二 修改next cdn 首先进入hexo博客首页 xff0c F12查看报错的 jsdelivr 地址
  • busuanzi.ibruce.info 有时候报502,怎么解决

    一 现象 https busuanzi ibruce info 访问经常出现502 导致个人博客的访问人数无法正常显示 二 如何解决 用chrome 打开busuanzi ibruce info xff0c 点url链接前的那个锁 然后看到
  • BigDecimal和Double的区别

    Double span class token number 0 3 span span class token number 0 2 span span class token operator 61 span span class to
  • 对科里奥利力的理解

    首先创造一个情景 xff0c 方便理解 假设你站在一个这样的封闭靶场 xff08 全封闭 xff09 上 xff0c 这个靶场在以大小为 的角速度做匀速圆周运动 xff08 速度不大不小 xff0c 你感觉不到 xff0c 而且靶场没风 x
  • typora使用技巧

    1 Typora vue theme的介绍与下载 typora vue theme是参考了Vue文档风格而开发的一个 Typora 自定义主题 点击此处下载 2 如何安装 a 下载本主题中的vue css vue dark css文档和包含
  • 序列化什么意思

    序列化就是一种用来处理对象流的机制 xff0c 将对象转化成字节序列后可以保存在磁盘上 xff0c 或通过网络传输 xff0c 以达到以后恢复成原来的对象
  • mybatis plus 事务回滚总结

    https www cnblogs com c2g5201314 p 13163097 html
  • throw 和 try catch 的区别

    try catch是直接处理 xff0c 处理完成之后程序继续往下执行 xff0c throw则是将异常抛给它的上一级处理 xff0c 程序便不往下执行了
  • throw的异常日志会打印吗

    throw 就是要把异常继续抛出 xff0c 要么由上层方法解决 xff0c 要么会终止程序运行
  • java assert什么意思

    assert 意为断言的意思 xff0c 这个关键字可以判断布尔值的结果是否和预期的一样 xff0c 如果一样就正常执行 xff0c 否则会抛出AssertionError assert 的使用 xff1a span class token
  • throw和throws的区别

    throws xff1a 用来声明一个方法可能产生的所有异常 xff0c 不做任何处理而是将异常往上传 xff0c 谁调用我我就抛给谁 用在方法声明后面 xff0c 跟的是异常类名 可以跟多个异常类名 xff0c 用逗号隔开 表示抛出异常
  • 1024有感

    2022 10 24 1024节日快乐 xff01 好好学习 xff0c 天天向上 x1f600
  • 互联网项目一般几轮测试

    第一轮测试 xff1a 要覆盖所有测试用例 所有功能都要跑一遍 第二轮测试 xff1a 重点功能的测试 还要把第一轮测试发现的问题 xff0c 根据开发修改完成的结果 xff0c 进行验证 最后一轮是回归测试 xff1a 验证所有bug是否

随机推荐