如何使用通用类型数组设置Prop?
我有一个选择的组件,我要做的是以这种方式实现组件,以便我可以通过任何类型的数组。怎么办?通用应在哪里定义?我使用< script设置>
和typecript。
这就是我尝试定义道具的方式:
const props = defineProps<T[]>({
options: {type: Array as PropType<T[]>, required: true }
})
I have a select component and what I am trying to do is to implement the component in that way so I can pass an array of any type. How can this be done? Where should the generic be defined? I am using <script setup>
with typescript.
This is how I tried to define the props:
const props = defineProps<T[]>({
options: {type: Array as PropType<T[]>, required: true }
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您正在使用Typescript,因此VUE允许您这样做:
https://vuejs.org/api/sfc-script-setup.html#typescript-only-features
Since you're using TypeScript, Vue allows you to do this:
https://vuejs.org/api/sfc-script-setup.html#typescript-only-features
你可以这样使用
you can use like this
您可以使用类似的通用道具类型:
了解更多信息: https:// vuejs。 org/api/sfc-script-stup.html#generics
You can use generic prop types like so:
Learn more: https://vuejs.org/api/sfc-script-setup.html#generics