您的位置:首页 > 财经 > 金融 > 网站搭建心得体会_网站架设软件_南宁网_成都网站建设系统

网站搭建心得体会_网站架设软件_南宁网_成都网站建设系统

2025/7/31 7:54:48 来源:https://blog.csdn.net/Supposelll/article/details/147425896  浏览:    关键词:网站搭建心得体会_网站架设软件_南宁网_成都网站建设系统
网站搭建心得体会_网站架设软件_南宁网_成都网站建设系统

前言

使用TS、Vue3组织组件中传入props的通用方式

步骤

步骤1:使用 defineProps 定义 Props

使用interface定义props中各项的类型:

// 组件List.vue// 定义 Props 类型和接口
interface ListItem {name: string;time: string;content: {status: number;name: string;}[];
}// 使用 defineProps 定义 Props
const props = defineProps<{listData?: ListItem[]; // listData 属性为可选的 ListItem 数组类型
}>();

步骤二:设置默认值

使用Vue3中的withDefaults定义props默认值

// 组件List.vue
// 定义 Props 类型和接口
interface ListItem {name: string;time: string;content: {status: number;name: string;}[];
}// 使用 withDefaults 设置默认值
const props = withDefaults(defineProps<{listData?: ListItem[]; // listData 属性为可选的 ListItem 数组类型}>(),{listData: () => [], // 设置 listData 的默认值为空数组}
);

之后组件中即可使用props.listData来访问props中的值。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com