JavaScript/vue.js/defineProps:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
创建页面,内容为“== 默认值 == vue 3.5.0+ 响应式 Props 解构 <syntaxhighlight lang="typescript"> const { type = 'number', useColor = false, usePrefix = false, value, title = '', color = '#ffffffff', } = defineProps({ type: String as PropType<'number' | 'string'>, useColor: Boolean, usePrefix: Boolean, value: [Number, String] as PropType<number | string>, title: String, color: [String, undefined] as PropType<ColorInputWithoutInstance>, }) </syntaxh…”
 
Kangkk​(留言 | 贡献)
 
第1行: 第1行:
== 默认值 ==
== 默认值 ==
vue 3.5.0+ 响应式 Props 解构
vue 3.5.0+ [https://cn.vuejs.org/guide/components/props.html#reactive-props-destructure 响应式 Props 解构]


<syntaxhighlight lang="typescript">
<syntaxhighlight lang="typescript">
第20行: 第20行:
</syntaxhighlight>
</syntaxhighlight>


vue 3.4.0- 使用 withDefaults 提供默认值
vue 3.4.0- 使用 [https://cn.vuejs.org/api/sfc-script-setup#default-props-values-when-using-type-declaration withDefaults] 提供默认值


<syntaxhighlight lang="typescript">
<syntaxhighlight lang="typescript">

2025年4月11日 (五) 08:17的最新版本

默认值

vue 3.5.0+ 响应式 Props 解构

const {
  type = 'number',
  useColor = false,
  usePrefix = false,
  value,
  title = '',
  color = '#ffffffff',
} = defineProps({
  type: String as PropType<'number' | 'string'>,
  useColor: Boolean,
  usePrefix: Boolean,
  value: [Number, String] as PropType<number | string>,
  title: String,
  color: [String, undefined] as PropType<ColorInputWithoutInstance>,
})

vue 3.4.0- 使用 withDefaults 提供默认值

const props = withDefaults(
  defineProps<{
    type?: 'number' | 'string'
    useColor?: boolean
    usePrefix?: boolean
    value?: number | string
    title?: string
    color?: ColorInputWithoutInstance
  }>(),
  {
    title: '',
    type: 'number',
    useColor: false,
    usePrefix: false,
    value: (props: any) => (props.type === 'number' ? 0 : ''),
    color: '#ffffffff',
  },
)