如何使用 typescript 在 vue3 设置脚本中导入类型?

Ant*_*Lun 5 typescript vuejs3 vite

我想在打字稿中使用<script setup>带有 props 类型检查的功能。但似乎<script setup>没有将 RouteRecordRaw 作为类型导入,而是作为值导入?如果我在 vite 中运行此代码,浏览器控制台将打印错误:

“未捕获的语法错误:请求的模块'/node_modules/.vite/vue-router.js?v=52a879a7'不提供名为'RouteRecordRaw'的导出”

代码:

<script lang="ts" setup>
import { RouteRecordRaw } from "vue-router";
// VSCode will print an error: "RouteRecordRaw" only refers to a type, butisbeing used as a value here. (TS2693) 

import { defineProps } from "vue";

const props = defineProps<{
  routeList: Array<RouteRecordRaw>;
}>();

const renderRoutes = props.routeList;
</script>
Run Code Online (Sandbox Code Playgroud)

Mic*_*evý 15

用这个:

import type { RouteRecordRaw } from "vue-router";
Run Code Online (Sandbox Code Playgroud)

更多信息