历史管理器

    或者使用 registerRouterInjector 帮助函数:

    1. import Registry from '@dojo/framework/core/Registry';
    2. import { registerRouterInjector } from '@dojo/framework/routing/RouterInjector';
    3. import StateHistory from '@dojo/framework/routing/history/StateHistory';
    4. import routes from './routes';
    5. const registry = new Registry();
    6. registerRouterInjector(routes, registry);
    7. // creates and registers a router using the `StateHistory`
    8. registerRouterInjector(routes, registry, { HistoryManager: StateHistory });

    HashHistory 使用片段标识符(fragment identifier)来处理路由变更,比如 https://foo.com/#home 中的 home 就是路由的路径。因为 HashHistory 是默认管理器,因此不需要导入模块。

    StateHistory 使用浏览器的 history API 管理应用程序的路由变更。

    1. 重写 index.html 请求,以从应用程序根目录加载。
    2. 重写加载静态资源(.js.css等)的请求,以从应用程序根目录加载。

    注意: 当使用 @dojo/cli-build-app 的 选项时,本机制已经包含在其中(仅用于开发环境)。

    1. import { Router } from '@dojo/framework/routing/Router';
    2. import { StateHistory } from '@dojo/framework/routing/history/StateHistory';
    3. const router = new Router(config, { HistoryManager: StateHistory });

    MemoryHistory 不依赖任何浏览器 API,而是自己保存内部的路径状态。不要在生产应用程序中使用它,但在测试路由时却很有用。

    1. import renderer from '@dojo/framework/core/vdom';
    2. import { registerRouterInjector } from '@dojo/framework/routing/RouterInjector';
    3. import routes from './routes';
    4. import App from './App';
    5. const registry = new Registry();
    6. // creates a router with the routes and registers the router with the registry
    7. registerRouterInjector(routes, registry);
    8. r.mount({ registry });

    这些历史管理器的实现原理与适配器类似,这意味着可以通过实现历史管理器接口来实现自定义的历史管理器。