SuperAPI AI 工具集

tanstack-virtual-file-routes

通用与其它

查看项目

更新于 10-16 00:02

技能介绍

用途:@tanstack/virtual-file-routes 让开发者以编程方式构建 TanStack Router 的路由树,而不是依赖文件系统约定。适合需要显式控制路由结构、混合虚拟与物理路由,或在基于文件的路由目录中定义路由子树的场景。

主要能力:提供 rootRoute(file, children?)、index(file)、route(path, ...)、layout(file, children) 或 layout(id, file, children)、physical(pathPrefix, directory) 或 physical(directory),以及 defineVirtualSubtreeConfig(config)。route 支持三种签名:叶子路由(path + file)、分支路由(path + file + children)、仅路径前缀(无 file,用于把子路由归组到某 URL 段下)。layout 创建无路径布局路由,包裹子路由但不增加 URL 段。physical 把某个目录下的文件式路由挂载到指定 URL 前缀。defineVirtualSubtreeConfig 是用于文件式路由目录内 __virtual.ts 的类型辅助函数。

使用方式:安装 npm install @tanstack/virtual-file-routes。在路由配置文件中导入这些函数并导出路由树,然后在 TanStack Router 插件中通过 virtualRouteConfig 传入配置对象或文件路径,例如 tanstackRouter({ target: 'react', virtualRouteConfig: routes })。

限制与注意:类型完全推断,不要强制转换或标注推断值。文件路径相对于路由插件配置的 routesDirectory(默认 ./src/routes),不要使用绝对路径或项目根相对路径。physical() 传入的目录必须存在于 routesDirectory 内并遵循文件式路由约定。layout() 不添加 URL 段,需要 URL 段应使用 route()。资料未提供收费、价格、平台支持范围等更多信息。