使用 MatchDetails

    注意:所有示例都假设正在使用默认的 HashHistory 历史管理器。

    • queryParams: { [index: string]: string }: 获取匹配路由的查询参数。
    • 如果 URL 路径为 /#home?foo=bar&baz=42,那么 queryParams 对象将如下所示:
    1. {
    2. foo: 'bar',
    3. baz: '42'
    4. }

    params

    • params: { [index: string]: string }: 匹配路由的路径参数
    1. export default [
    2. {
    3. id: 'home',
    4. path: 'home/{page}',
    5. outlet: 'home'
    6. }
    7. ];
    • 如果 URL 路径为 /#home/aboutparams 对象将如下所示:
    • isExact(): boolean: 一个函数,用于指出路由是否与路径完全匹配。这可用于按条件渲染不同的部件或节点。
    1. export default [
    2. {
    3. id: 'home',
    4. path: 'home',
    5. outlet: 'home',
    6. children: [
    7. {
    8. id: 'about',
    9. outlet: 'about'
    10. }
    11. }
    12. ];
    • 根据上述的路由定义,如果 URL 路径设置为 /#home/about,那么对于 id 为“home”的 RouteisExact() 的值将为 false;如果是 home Route 的子 Route,且 id 为“about”时,isExact() 的值将为 true,如以下所示:

    src/App.tsx

    1. import { create, tsx } from '@dojo/framework/core/vdom';
    2. import Route from '@dojo/framework/routing/Route';
    3. const factory = create();
    4. export default factory(function App() {
    5. return (
    6. <div>
    7. <Route
    8. id="home"
    9. renderer={(homeMatchDetails) => {
    10. console.log('home', homeMatchDetails.isExact()); // home false
    11. return (
    12. <Route
    13. id="about"
    14. renderer={(aboutMatchDetails) => {
    15. console.log('about', aboutMatchDetails.isExact()); // about true
    16. return [];
    17. }}
    18. />
    19. );
    20. }}
    21. />
    22. </div>
    23. });

    isError()

    • isError(): boolean: 一个函数,用于指出路由是否与路径匹配错误。表示经过匹配后,没有找到匹配项。
    • 根据上述的路由定义,如果 URL 路径设置为 /#home/foo,则无法匹配到路由,所以 home RoutematchDetails 对象的 isError() 将返回 。导航到 /#home/#home/about 将返回 false,因为这两个路由已定义过。
    • type: 'index' | 'partial' | 'error': 路由的匹配类型,值为 indexpartialerror。不要直接使用 type,而是组合使用 isExactisError
    1. export default [
    2. {
    3. id: 'home',
    4. path: 'home',
    5. outlet: 'home',
    6. children: [
    7. id: 'about',
    8. path: 'about',
    9. outlet: 'about'
    10. ]
    11. }
    12. ];
    • 根据上述的路由定义,每个 outlet 对应的 type 值应为:

    router

    • router: RouterInterface: 路由实例,用于创建链接和触发路由变更。更多信息参考路由 API。

    src/routes.ts

    1. export default [
    2. {
    3. id: 'home',
    4. path: 'home',
    5. outlet: 'home',
    6. children: [
    7. {
    8. id: 'home-details',
    9. path: 'details',
    10. outlet: 'home-details'
    11. }
    12. ]