在项目中通过 declare namespace
来声明命名空间
我在这里创建了一个global.d.ts
文件,在该文件中声明了Menu
命名空间,这样就可以在全局使用Menu
类型了,无需import
引入
当然,你可以在项目中任何地方新建global.d.ts
(文件名自定义)并声明命名空间,即使放到views
文件夹也可以,当然,为了项目规范化,最好放到指定目录
/* ts命名空间namespace */
/* 路由-Menu */
declare namespace Menu {
interface MenuOptions {
path: string;
name: string;
redirect?: string;
component?: string | (() => Promise<unknown>);
meta: MetaType;
children?: MenuOptions[];
}
interface MetaType {
title: string;
isHide: boolean;
isKeepAlive: boolean;
isAffix: boolean;
isLink?: string;
isIframe?: boolean;
roles: array<string>;
icon?: string;
svgIcon?: string;
}
}
在页面中使用
<script setup lang="ts">
import IconCommon from "@/layout/components/Menu/icon-common.vue";
defineOptions({ name: "MenuItem" });
interface Props {
routeTree: Menu.MenuOptions[]; // 直接使用Menu命名空间
}
const props = withDefaults(defineProps<Props>(), {
routeTree: () => []
});
</script>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。