vue3.3优化了defineProps和defineEmits写法

针对defineProps的优化

  • 父组件调用
<template>
  <A :child="['yx']"></A>
</template>

<script setup lang="ts">

import A from './A.vue'

</script>

  • 子组件
普通方法获取props 传递过来的之
<template>
  <div>
    A component --- {{child}}
  </div>
</template>

<script setup lang="ts" name="A">

import { PropType } from 'vue'

// 普通方法
const props = defineProps({
    child:{
        type: Array as PropType<string[]>,
        required:true
    }
})
// 为 unkonw 缺少类型提示 为了解决这个问题 vue 提供了一个 propType 类型
// 添加了 PropType 后 props.child 会推断成 string类型数组
props.child

</script>

ts 泛型字面量获取 props 传递过来的值
/*  *** */

<script setup lang="ts" name="A">

// ts 泛型字面量方法
const props = defineProps<{
    child:string[]
}>()

props.child

</script>
vue3.3 对 defineProps的改进,新增泛型支持需要在script标签上加 generic=“T”,T为泛型
/*  **  */
<script generic="T" setup lang="ts" name="A">

// vue3.3 对 defineProps的改进,新增泛型支持需要在script标签上加 generic=“T”,T为泛型
// 如父组件传递过来刚开始是string,后面改成number,boolean等,子组件不用跟着改变
const props = defineProps<{
    child:T[]
}>()

props.child

</script>

defineEmits优化

  • 父组件
<template>

  <A @send="getName"></A>
</template>

<script setup lang="ts">

import A from './A.vue'

const getName = (child)=>{
  console.log('child:',child)
}

</script>
  • 子组件
<template>
  <button @click="send">验证emit</button>
</template>

普通函数defineEmits调用
<script setup lang="ts" name="A">

// 普通方法定义一个send方法
const emits = defineEmits(['send'])

const send = ()=>{
    emits('send','hello yx')
}
ts 写法
<script setup lang="ts" name="A">

// ts 写法
const emits = defineEmits<{
    // event 类似于形参数, name 是函数 的形参
    (event:string,name:string):void
}>()
const send = ()=>{
    emits('send','hello yx')
}

</script>
vue3.3 写法更加精简
<script setup lang="ts" name="A">

const emits = defineEmits<{
        'send':[name:string]
}>()

const send = ()=>{
    emits('send','hello yx')
}
</script>

相关推荐

  1. vue3.3优化definePropsdefineEmits写法

    2024-04-04 01:48:04       39 阅读
  2. definePropsdefineEmits

    2024-04-04 01:48:04       22 阅读
  3. vue3中defineProps详解

    2024-04-04 01:48:04       42 阅读
  4. Vue3 defineProps的使用

    2024-04-04 01:48:04       24 阅读
  5. 学习Vue3的defineProps方法

    2024-04-04 01:48:04       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-04 01:48:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-04 01:48:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-04 01:48:04       87 阅读
  4. Python语言-面向对象

    2024-04-04 01:48:04       96 阅读

热门阅读

  1. C# OAuth单点登录的实现

    2024-04-04 01:48:04       39 阅读
  2. [ RV1108_LINUX] 关于如何调整cpu中vdd_core的电压

    2024-04-04 01:48:04       35 阅读
  3. uniapp 打开抖音小程序

    2024-04-04 01:48:04       36 阅读
  4. 使用的sql

    2024-04-04 01:48:04       40 阅读
  5. 网络安全:筑牢防线,抵御恶意攻击

    2024-04-04 01:48:04       41 阅读
  6. springboot

    2024-04-04 01:48:04       41 阅读
  7. Oracle控制文件管理

    2024-04-04 01:48:04       30 阅读
  8. Oracle联机日志文件管理

    2024-04-04 01:48:04       35 阅读
  9. dlib中rectangle与opencv的rect的区别

    2024-04-04 01:48:04       33 阅读
  10. 0基础如何进入IT行业?

    2024-04-04 01:48:04       31 阅读
  11. os模块篇(十一)

    2024-04-04 01:48:04       32 阅读
  12. 借助ChatGPT写作:打造学术论文中的亮点与互动

    2024-04-04 01:48:04       45 阅读