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>

posted on   ChoZ  阅读(7)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示