withDefaults

作用是给defineProps绑定默认值的api

父组件

<template>
    <TsSample :msg='msg' @on-updated='onUpdated' title='title' @on-delete='onDelete'/> 
</template>

子组件

<template>
  <h1>ts sample</h1>
  <p>{{ msg }}</p>
</template>
第一种方式哦:分离模式
interface Props{
    msg?:(string|number|boolean),
     title?:string[]
 }
 withDefaults(defineProps<Props>(),{
    msg:'hello',
    title:()=>['one','two']
 })
第二种方式:组合模式
withDefaults(
  defineProps<{ msg?: (string | number | boolean), title?: string }>(),{
      msg:'hello vite',
      title:'默认标题'
  }
);
使用
//利用接口声明类型
interface testType {
  name: string;
  age: number;
}
const props = withDefaults(
  defineProps<{
    text: testType;
  }>(),
  {
    text: {
      name: "flying_dark_feather",
      age: 20,
    },
  }
);
//但是在使用的时候需要利用传参的形式传递实际数据,否则会被识别成字符串而报错
GitHub 加速计划 / vu / vue
105
18
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:1 个月前 )
9e887079 [skip ci] 1 年前
73486cb5 * chore: fix link broken Signed-off-by: snoppy <michaleli@foxmail.com> * Update packages/template-compiler/README.md [skip ci] --------- Signed-off-by: snoppy <michaleli@foxmail.com> Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 1 年前
Logo

新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐