For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt, and this page is available as Markdown at /zh/apis/app/runtime/router/memory-route.md.
  • 简体中文
  • Memory Route API

    用于为 <ModernRoot /> 配置和查询内存路由,均从 @modern-js/runtime/router 导出。常规路由 API(useNavigate、useLocation、<Link>、useLoaderData 等)用法保持不变。

    MemoryRouteProvider

    为包裹的 <ModernRoot /> 子树启用内存路由。

    import { MemoryRouteProvider } from '@modern-js/runtime/router';
    
    <MemoryRouteProvider
      initialEntries={['/', '/detail/42']}
      initialIndex={1}
      basename="/shop"
    >
      <ModernRoot />
    </MemoryRouteProvider>;

    Props

    属性类型说明
    entryPathstring初始路由路径,例如 '/detail/42?tab=info'。
    initialEntriesMemoryRouteEntry[]初始历史栈。
    initialIndexnumberinitialEntries 中的当前激活索引(默认指向最后一项)。
    initialStateunknown配合 entryPath 使用的初始 location.state。
    basenamestring路由前缀。
    configMemoryRouterConfig简写或完整配置对象;显式传入的 prop 优先级更高。

    ModernRoot memoryRoute

    直接在根组件上启用或覆盖内存路由:

    <ModernRoot memoryRoute="/beta" />

    多处同时配置时的优先级:props.memoryRoute > <MemoryRouteProvider> > router.memory。

    useMemoryRoute

    在路由组件内读取当前内存路由状态。

    function useMemoryRoute(): {
      enabled: boolean;
      initialEntries?: MemoryRouteEntry[];
      initialIndex?: number;
      basename?: string;
    };
    import { useMemoryRoute } from '@modern-js/runtime/router';
    
    const { enabled, basename } = useMemoryRoute();