dmo*_*iba 3 typescript vue.js vuejs3 vue-composition-api vue-script-setup
在我的代码中,我已经(或希望)在某些时候使用复杂的 prop 类型。下面是我想到的示例:
enum Country {
[...]
}
interface IPerson {
firstname: string;
lastname: string;
}
interface IAddress {
street: string;
houseNo: string;
city: string;
postalCode: string;
country: number;
}
interface ITestComponentProps {
person: IPerson;
addresses: Array<IAddress>;
}
Run Code Online (Sandbox Code Playgroud)
那么我想用这一切来实现的TestComponent.vue是:
[...]
const props = defineProps<ITestComponentProps>();
[...]
Run Code Online (Sandbox Code Playgroud)
我现在期望的是某种错误消息或任何告诉我在尝试如下操作时没有提供正确结构的道具的信息:
[...]
<TestComponent :props="'foo'" />
[...]
Run Code Online (Sandbox Code Playgroud)
Vue 的文档中有如下说明:
目前不支持复杂类型和从其他文件导入类型。将来有可能支持类型导入。
有谁知道是否有解决方法来获得所需的行为或何时支持复杂类型?
在Vue 版本 < 3.3中,语法如下:
const props = defineProps<ITestComponentProps>();
Run Code Online (Sandbox Code Playgroud)
仅支持:
建议使用其他语法:
import {PropType} from 'vue'
const props=defineProps({
person:{
type:Object as PropType<Person>
}
})
Run Code Online (Sandbox Code Playgroud)
因为这个语法:
此评论归功于Linus borg
| 归档时间: |
|
| 查看次数: |
3855 次 |
| 最近记录: |