vue3+ts+setup,props写法

js切换到ts的时候,setup一下子不知道怎么写了,各种报错,记录一下

写法一

import defaultImg from '@/assets/images/defaultImg.png'
const props = defineProps({
  src: {
    type: String,
    default: ''
  },
  title: {
    type: String,
    default: '图片'
  },
  defaultImg: {
    type: String,
    default: defaultImg
  }
})

写法二

import { reactive } from 'vue'
import defaultImg from '@/assets/images/defaultImg.png'

interface DataProps {
  src: string
  title: string
  defaultImg: string
}
const props: DataProps = reactive({
  src: '',
  title: '图片',
  defaultImg
})

使用方式

<template>
  <img :src="props.src" :title="props.title" @error="imgError"/>
</template>

 完整示例二:

<template>
  <van-icon
    class="iconfont"
    class-prefix='icon'
    :name='props.name'
    :dot='props.dot'
    :badge='props.badge'
    :color='props.color'
    :size='props.size'
    :class='props.className'
    @click="emit('click')" />
</template>
<script setup lang="ts">
import { reactive } from 'vue'
interface DataProps {
  name: string
  dot: boolean
  badge: number | string
  color: string
  size: number | string
  className: string
}
const props: DataProps = reactive({
  name: '',
  dot: false,
  badge: null,
  color: 'inherit',
  size: 'inherit',
  className: ''
})
</script>

猜你喜欢

转载自blog.csdn.net/weixin_44821114/article/details/133324061