vue3使用笔记
vue3使用笔记
vue3中父子组件之间的传值
-
父传子
子组件使用父组件的参数和事件
子组件(仅传参)
<template> <div class="son">子组件: {{name}}</div> </template> <script setup> import {defineProps} from 'vue' const props = defineProps({ name: { type: String, default: '默认值' } }) </script>
父组件(仅传参)
<template> <son :name="msg" /> <son /> </template> <script setup> import { ref } from "vue"; import son from './son.vue' const msg = ref('父组件') </script>
总结: 子组件引入defineProps, 定义props,然后就可以直接在模板中使用了, 在defineProps中可以设置默认值
父组件直接引入传参即可
子组件(子组件调用父组件事件)
<template> <div class="son"> <div> 子组件: {{name}} </div> <div> <button @click="handleClick">子按钮</button> </div> </div> </template> <script setup> import {ref, defineProps, defineEmits} from 'vue' const props = defineProps({ name: { type: String, default: '默认值' } }) const count = ref(10) const emit = defineEmits() const handleClick = () => { emit('handleCount', count.value ++) } </script>
父组件(子组件调用父组件事件)
<template> <son :name="msg" @handleCount="handleCount" /> </template> <script setup> import { ref } from "vue"; import son from './son.vue' const msg = ref('父组件') const handleCount = (val) => { console.log('val', val) } </script>
总结:
- 子组件引入:defineEmits
- 通过defineEmits定义emit
- 通过emit('handleCount', count.value ++)来调用定义在父组件的方法
- 父组件引用子组件并定义方法handleCount,在通过@handleCount="handleCount"的方式传给子组件
子组件调用父组件事件(方法2)
子组件
<template> <div class="son"> <div> 子组件: {{name}} </div> <div> <button @click="handleClick">子按钮</button> </div> </div> </template> <script setup> import {ref, defineProps, defineEmits} from 'vue' const props = defineProps({ name: { type: String, default: '默认值' }, handleCount: { type: Function, default: undefined } }) const count = ref(10) const handleClick = () => { props.handleCount && props.handleCount(count.value ++) } </script>
父组件
<template> <son :name="msg" :handleCount="handleCount" /> </template> <script setup> import { ref } from "vue"; import son from './son.vue' const msg = ref('父组件') const handleCount = (val) => { console.log('val', val) } </script>
总结:
- 子组件中,将事件定义为属性,通过defineProps
- props.handleCount && props.handleCount(count.value ++)这样使用
- 父组件中定义好事件后, 同样将事件以属性的方式传入, :handleCount="handleCount"
-
子组件修改props属性的值
子组件 <template> <div class="son"> <div> 子组件: {{name}} </div> <div> <button @click="handleClick">修改props</button> </div> </div> </template> <script setup> import {ref, defineProps, defineEmits} from 'vue' const props = defineProps({ name: { type: String, default: '默认值' }, }) const count = ref(10) const emit = defineEmits() const handleClick = () => { emit('update:name', '修改后的') } </script>
父组件
<template> <son v-model:name="msg" /> </template> <script setup> import { ref } from "vue"; import son from './son.vue' const msg = ref('父组件') </script>
总结:
- 子组件通过props接受父组件传过来的值
- 通过emit('update:name')修改prop的值
- 父组件传值方式改变: v-model:name="msg", 不能直接 :name="msg"
-
父组件使用子组件的方法
子组件
<template> <div class="son"> <div> 子组件: {{name}} </div> <div> <button @click="handleClick">修改props</button> </div> </div> </template> <script setup> import {ref, defineProps, defineEmits, defineExpose} from 'vue' const props = defineProps({ name: { type: String, default: '默认值' }, handleCount: { type: Function, default: undefined } }) const count = ref(10) const emit = defineEmits() const handleClick = () => { emit('update:name', '修改后的') } const sonFunc = () => { console.log('123') } defineExpose({sonFunc}) </script>
父组件
<template> <div> <button @click="handleChild">父组件</button> {{msg}} <son ref="sonRef" v-model:name="msg" /> </div> </template> <script setup> import { ref } from "vue"; import son from './son.vue' const msg = ref('父组件') const sonRef = ref() const handleChild = () => { sonRef.value.sonFunc() } </script>
总结: 就是父组件使用子组件的ref.value.方法
- 子组件定义事件, 通过引入defineExpose将定义的事件暴露出去,defineExpose({sonFunc})
- 父组件中定义组组件ref const sonRef = ref()
- 父组件使用子组件方法: sonRef.value.sonFunc()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!