uniapp vue3 ts 接值 获取defineprops的值
时间: 2024-12-29 12:30:41 浏览: 95
在UniApp中,结合Vue3和TypeScript,当使用`defineProps`来自定义组件属性并获取用户传递的值时,你需要在组件的script部分定义props,并在setup()钩子函数中访问它们。以下是基本步骤:
1. 定义props:
```typescript
import { defineComponent, PropType } from 'vue';
export default defineComponent({
props: {
// 使用PropType指定数据类型
name: {
type: String as PropType<string>, // 类型可以是String、Number等
required: true, // 属性是否必需
default: '默认值', // 默认值
},
age: {
type: Number,
default: 0,
},
}
});
```
2. 在setup()函数中获取prop值:
```typescript
setup(props) {
const fullName = computed(() => `${props.name} 用户`);
return {
fullName,
// 访问其他prop同理
userAge: props.age,
};
}
```
当你在模板中使用这些计算属性时,就可以直接通过`this.fullName`或`this.userAge`来获取从父组件传递过来的值了。
阅读全文
相关推荐

















