Vue3:【props、emit 和 components】 在 setup 语法糖和非语法糖中的使用

目录

  • [1 props](#1 props)
    • [1.1 setup 语法糖](#1.1 setup 语法糖)
    • [1.2 非语法糖](#1.2 非语法糖)
  • [2 emit](#2 emit)
    • [2.1 setup 语法糖](#2.1 setup 语法糖)
    • [2.2 非语法糖](#2.2 非语法糖)
  • [3 components 组件](#3 components 组件)
    • [3.1 语法糖](#3.1 语法糖)
    • [3.2 非语法糖](#3.2 非语法糖)

1 props

1.1 setup 语法糖

ts 复制代码
<script lang="ts" setup>

interface PersionItem {
  id: number
  value: number
  imageSrc: string
  label: string
}
const props = defineProps({
  personData: {
    type: Array as () => PersionItem[],
    default: () => []
  }
})

console.log(props.personData)

1.2 非语法糖

typescript 复制代码
<script lang="ts">

interface Option {
  imageSrc: String
  fileName: String
  modifierInfo: String
  path: String
  origin: String
}
export default {
  props: {
    listData: {
      type: Array as () => Option[],
      default: () => []
    }
  },
  setup(props, { emit }) {
    console.log(props.listData)
    return {}
  }
}
</script>

2 emit

2.1 setup 语法糖

typescript 复制代码
<script lang="ts" setup>
import { defineEmits } from 'vue'
// 定义
const emit = defineEmits(['checkPerson'])

const check = () => {
  emit('checkPerson', personID.value)
}
</script>

2.2 非语法糖

typescript 复制代码
<script lang="ts">
export default {
  emits: ['focusEvent', 'blurEvent'],
  setup(props, { emit }) {
    // const emit = defineEmits(['debouncedInput', 'focusEvent', 'blurEvent'])
    // 获得焦点事件
    const focusFn = () => {
      emit('focusEvent')
    }
    // 失去焦点事件
    const blurFn = () => {
      emit('blurEvent')
    }

    return {  focusFn, blurFn }
  }
}
</script>

3 components 组件

3.1 语法糖

引入后直接用

3.2 非语法糖

typescript 复制代码
<script lang="ts">
import fileNameItem from './fileNameItem.vue'

export default {
  // 局部注册组件
  components: {
    fileNameItem
  },
  setup(props, { emit }) {
    return {}
  }
}
</script>
相关推荐
clownAdam34 分钟前
Chrome性能优化秘籍
前端·chrome·性能优化
@Kerry~38 分钟前
phpstudy .htaccess 文件内容
java·开发语言·前端
nueroamazing1 小时前
PPT-EA:PPT自动生成器
vue.js·python·语言模型·flask·大模型·项目·ppt
WebDesign_Mu2 小时前
为了庆祝2025英雄联盟全球总决赛开启,我用HTML+CSS+JS制作了LOL官方网站
javascript·css·html
@PHARAOH2 小时前
WHAT - 前端性能指标(交互和响应性能指标)
前端·交互
噢,我明白了2 小时前
前端js 常见算法面试题目详解
前端·javascript·算法
im_AMBER2 小时前
Web 开发 30
前端·笔记·后端·学习·web
学编程的小虎2 小时前
用 Python + Vue3 打造超炫酷音乐播放器:网易云歌单爬取 + Three.js 波形可视化
开发语言·javascript·python
Jonathan Star2 小时前
Webpack 打包优化与骨架屏结合:双管齐下提升前端性能与用户体验
前端·webpack·ux
做好一个小前端3 小时前
后端接口获取到csv格式内容并导出,拒绝乱码
前端·javascript·html