Skip to content

路由类型

开启 dts 后,uniAutoPages 会根据扫描到的页面生成类型声明文件。

生成内容

ts
import type { DefinePageConfig } from "vite-plugin-uni-inject";

// Auto-generated by vite-plugin-uni-inject. Do not edit.

/** 提取路径 */
export type ExtractPath<T extends string, Prefix extends string> = T extends \`\${Prefix}\${infer P}\` ? P : never;

/** 路由路径 */
export type RoutePath = ${routeUnion};

declare global {
  /**
   * Vue \`<script setup>\` 宏定义 uniapp 页面级配置。
   */
  function definePage(config: DefinePageConfig): void;
}

使用方式

把生成文件放入 tsconfig.jsoninclude 范围后,就可以在页面中直接使用 definePage,也可以在业务代码中复用 RoutePath 类型。

ts
import type { RoutePath } from "@/types/uni-pages";

function navigateTo(url: RoutePath) {
  uni.navigateTo({ url });
}

禁用生成

如果不需要生成类型声明,可以在 uniAutoPages 中把 dts 配置为空字符串。

Released under the MIT License.