defineProps,defineEmits,defineExport
<template>
<PropsEmitExport ref="propsEmitExportRef" :father="'标题'" @toFatherEmit="getChildData" @toFastherEmits="getChildDataTwo"></PropsEmitExport>
</template>
<script setup lang='ts'>
import PropsEmitExport from '../study/defineProps,defineEmit,defineExport/compontent.vue'
import { ref } from 'vue'
const getChildData = (data: string) => {
console.log(data)
}
const getChildDataTwo = (data: string) => {
console.log(data)
}
// 通过ref获取子组件的暴露的属性
const propsEmitExportRef = ref<InstanceType<typeof PropsEmitExport>>() // InstanceType是ts的一个内置类型,获取一个类的实例类型
console.log(propsEmitExportRef.value?.name); // 可以通过代码提示暴露的属性
</script>
<style lang="scss">
</style>
<template>
<div>
<!-- {{ paops.father }} js语法 -->
{{ father }} ts语法
</div>
<button @click="toFather">改变1</button>
<button @click="toFatherTwo">改变2</button>
</template>
<script setup lang='ts'>
// defineProps, defineEmits, defineExpose 在setup语法中不需要引入
//ts 带默认值的语法,不需要定义变量,直接解析
withDefaults(defineProps<{
father: string
}>(),{
father: 'father'
})
// js语法,需要定义成变量,通过paops.father获取
// const paops = defineProps({
// father: {
// type: String,
// default: 'father'
// }
// })
// js语法
// const emit = defineEmits(['toFatherEmit', 'toFastherEmits'])
// ts语法
const emit = defineEmits<{
(e: 'toFatherEmit', data: string): void
(e: 'toFastherEmits', data: string): void
}>()
const toFather = () => {
emit('toFatherEmit', '我是子组件传递的数据')
}
const toFatherTwo = () => {
emit('toFastherEmits', '我是子组件传递的数据2')
}
// 暴露给父组件,父组件通过ref获取
defineExpose({
name: 'PropsEmitExport'
})
</script>
<style scoped lang='scss'>
</style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现