vue3+ts props
时间: 2023-08-29 19:11:00 浏览: 98
vue3+ts组件练习文件
In Vue 3 with TypeScript, props can be defined in the following way:
```typescript
import { defineComponent, PropType } from 'vue';
export default defineComponent({
name: 'MyComponent',
props: {
propA: {
type: String as PropType<string>,
required: true,
default: 'default value',
validator: (value: string) => {
// custom validation logic
return value.length > 0;
}
},
propB: {
type: [String, Number] as PropType<string | number>,
default: 100
}
}
});
```
In the example above, `propA` is a required string prop with a default value of `'default value'` and a custom validator function that checks if the value has a length greater than 0. `propB` is a prop that can be either a string or a number, with a default value of `100`.
Note that the `PropType` type is used to specify the type of the prop, and can accept a single type or an array of types. Also, the validator function should return `true` if the value is valid, and `false` otherwise.
阅读全文