我能做到
defineComponent({
props: {
name: { type: String as PropType<string> }
}
})
告诉vue3我的props
类型是{ name: string }
,但是如果我有几个组件具有相同的props类型,我如何共享定义?
如果我在中定义道具:
const props = {
name: String as PropType<string>
}
然后像这样使用它:
defineComponent({
props: props,
})
它不能工作,我在道具的设置函数中得到的类型不正确。
发布于 2021-05-21 20:29:47
为defineComponent
提供的props选项是一个纯js对象,仅用于类型注释目的,因此您可以使用javascript中的任何技术在对象之间共享结构:
// in common.ts
export const commonProps = {
name: { type: String as PropType<string> }
}
// in your component.vue
import commonProps from "./common.ts";
defineComponent({
props:{
...commonProps,
extra: { }
}
})
发布于 2020-09-11 15:01:22
如果您正在使用带有Vue 2的组合API来准备切换到Vue 3,那么您必须使用该包中的PropType
,而不是vue
包。
// Wrong for Vue 2
import Vue, { PropType } from 'vue'
import { defineComponent } from '@vue/composition-api'
// Right for Vue 2
import { defineComponent, PropType } from '@vue/composition-api'
https://stackoverflow.com/questions/63335247
复制相似问题