【vue3 <script setup> props 使用与讲解】defineProps、withDefaults | ts类型限制、默认值设置

  • 本章主要涉及api内容:defineProps、withDefaults;
  • defineProps  只能是要么使用`运行时声明`,要么使用`typescript类型声明`。同时使用两种声明方式会导致编译报错。;
  • defineProps、withDefaults 是只在 <script setup> 语法糖中才能使用的编译器宏。他不需要导入且会随着 <script setup> 处理过程一同被编译掉。

  • withDefaults 只能与基于类型的defineProps声明一起使用;


例子1 > 运行时声明 的方式只能设置参数类型、默认值、是否必传、自定义验证。报错为控制台warn警告。

若想设置[ 编辑器报错、编辑器语法提示 ]则需要使用类型声明的方式。

<script lang='ts' setup>
const props = defineProps({
  child: {
    type:String, // 参数类型
    default: 1, //默认值
    required: true, //是否必传
    validator: value => {
      return value >= 0 // 除了验证是否符合type的类型,此处再判断该值结果是否符合验证
    }
  },
  sda: String, //undefined
  strData: String,
  arrFor: Array
})
</script>

子组件声明了的 props ,若父组件未传,则该值为 undefined 


例子2 > 类型声明的方式1:defineProps 单独使用该api,只能设置是否必传和参数类型。(利用TS特性)

<script lang='ts' setup>
const props = defineProps<{
  either: '必传且限定'|'其中一个'|'值', // 利用TS:限定父组件传 either 的值
  child?: string|number,
  strData?: string,
  arrFor: any[]
}>();
console.log(props);
</script>

相较于例子1,该写法只能设置参数类型、父组件对应 prop 是否该必传的功能。 

传值有误时,控制台报warn警告,还提供编辑器报错功能。

提供编辑器代码提示的功能。


 例子3 > 类型声明的方式2:在类型方式1的基础上,增加了设置 prop 默认值

<script lang='ts' setup>
interface Props {
  either: '必传且限定'|'其中一个'|'值', // 利用TS:限定父组件传 either 的值
  child: string|number,
  sda?: string, // 未设置默认值,为 undefined
  strData: string,
  msg?: string
  labels?: string[],
  obj?:{a:number}
}
const props = withDefaults(defineProps<Props>(), {
  msg: 'hello',
  labels: () => ['one', 'two'],
  obj: () => { return {a:2} }
})
</script>

注意:默认值为引用类型的,需要包装一个函数 return 出去。


QQ交流群:522976012  ,欢迎来玩

聚焦vue3,但不限于vue,任何前端问题,究其本质,值得讨论,研究与学习。
 

Vue 3中,使用`<script lang=&#39;ts&#39; setup>`标签可以声明props。有几种不同的方式可以声明props。 1. 使用`defineProps`API:此方式只能设置参数类型和是否必传。在`defineProps`中定义一个类型,可以使用TypeScript特性进行限定。例如: ```javascript const props = defineProps<{ either: &#39;必传且限定&#39;|&#39;其中一个&#39;|&#39;值&#39;, child?: string|number, strData?: string, arrFor: any[] }>(); ``` 2. 使用`withDefaults`和`defineProps`:在第一种方式的基础上,增加了设置prop默认值的功能。例如: ```javascript interface Props { either: &#39;必传且限定&#39;|&#39;其中一个&#39;|&#39;值&#39;, child: string|number, sda?: string, strData: string, msg?: string, labels?: string[], obj?:{a:number} } const props = withDefaults(defineProps<Props>(), { msg: &#39;hello&#39;, labels: () => [&#39;one&#39;, &#39;two&#39;], obj: () => { return {a:2} } }); ``` 3. 运行时声明的方式:只能设置参数类型默认值、是否必传、自定义验证。此方式报错为控制台warn警告,无法提供编辑器代码提示功能。例如: ```javascript const props = defineProps({ child: { type:String, default: 1, required: true, validator: value => { return value >= 0 } }, sda: String, strData: String, arrFor: Array }); ``` 综上所述,Vue 3的`<script lang=&#39;ts&#39; setup>`标签可以用来声明props,提供不同的方式来设置参数类型默认值、是否必传和自定义验证。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【vue3script setupprops 使用讲解definePropswithDefaults 类型限制默认值设置](https://blog.csdn.net/m0_67401228/article/details/123304831)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值