typescript - 使用 TypeScript 输入 Vue.js 3 的道具
问题描述
我正在尝试使用组合 API 在 Vue 3 组件中输入提示我的道具。
所以,我正在这样做:
<script lang="ts">
import FlashInterface from '@/interfaces/FlashInterface';
import { ref } from 'vue';
import { useStore } from 'vuex';
export default {
props: {
message: {
type: FlashInterface,
required: true
}
},
setup(props): Record<string, unknown> {
// Stuff
}
};
我的FlashInterface
样子是这样的:
export default interface FlashInterface {
level: string,
message: string,
id?: string
}
此界面运行良好,但在这种情况下我收到此错误:
ERROR in src/components/Flash.vue:20:10
TS2693: 'FlashInterface' only refers to a type, but is being used as a value here.
18 | props: {
19 | message: {
> 20 | type: FlashInterface,
| ^^^^^^^^^^^^^^
21 | required: true
22 | }
23 | },
我不明白为什么 TypeScript 认为这是一个值。
我错过了什么?
解决方案
您应该将它与PropType
从 vue 导入的一起使用,例如Object as PropType<FlashInterface>
:
import FlashInterface from '@/interfaces/FlashInterface';
import { ref,PropType, defineComponent } from 'vue';
import { useStore } from 'vuex';
export default defineComponent({
props: {
message: {
type: Object as PropType<FlashInterface>,
required: true
}
},
setup(props) {
// Stuff
}
});
注意:您应该使用创建组件defineComponent
来获得类型推断。
推荐阅读
- javascript - 将整个数组从一个函数传递到另一个函数 JavaScript
- java - 如何将复杂的 Java Stream 转换为循环?
- c++ - 在旋转和排序的数组中搜索
- javascript - 如何在退出时删除所有 Electron 膨胀软件?
- wordpress - 正则表达式向 URL 添加尾部斜杠,除非 URL 以文件扩展名结尾
- python - Pyplot:将线条样式更改为箭头
- javascript - Jquery在下拉列表的列中选择具有特定文本的行
- scala - Spark java.lang.NoClassDefFoundError: org/apache/spark/sql/execution/datasources/v2/FileDataSourceV2
- javascript - AngularJS:如何有条件地应用两个过滤器?
- discord.js - 消息收集器不值得