首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多级静态路由结构对路由器v4的反应

多级静态路由结构对路由器v4的反应
EN

Stack Overflow用户
提问于 2018-03-09 21:37:31
回答 1查看 391关注 0票数 0

我正在努力研究多层体系结构,这样我就可以有可能有多条通向同一组件的路径,并且仍然能够到达其他组件。

我对react router (router的静态版本)的理解有点有限,而且由于文档看起来不太透彻,我希望您能帮我。

下面是路由配置:

代码语言:javascript
复制
const routes = [
    {
        component : Root,
        routes    : [
            {
                path      : '/*',
                component : Layout,
                routes    : [
                    {
                        path      : '/',
                        exact     : true,
                        component : ComponentOne,
                    },
                    {
                        path      : '/route-2,
                        exact     : true,
                        component : ComponentTwo,
                    },
                    {
                        path      : '/complex-route',
                        component : ComplexComponent,
                        routes    : [
                            {
                                path      : '/complex-route/:all',
                                component : ComplexComponentTabAll,
                            },
                            {
                                path      : '/complex-route/tab-1',
                                exact     : true,
                                component : ComplexComponentTabOne,
                            },
                            {
                                path      : '/complex-route/tab-2',
                                exact     : true,
                                component : ComplexComponentTabTwo,
                            },
                        ],
                    },
                    {
                        path      : '*',
                        exact     : true,
                        component : NotFound,
                    },
                ],

            },
            {
                path      : '*',
                component : NotFound,
            },
        ],
    },
];

// ============================================================
// Exports
export default routes;

以下是组件布局:

代码语言:javascript
复制
const Layout = ({ route }) => (
    <div>
        <Menu />
        <MainBlock>
            <Header>
                {/*some sstuff goes here */}
            </Header>
            <Theatre>
                {renderRoutes(route.routes)}
            </Theatre>
        </MainBlock>
    </div>
);

下面是一个复杂组件的示例

代码语言:javascript
复制
const ComplexeComponent = ({ route }) => (
    <div>
        { renderRoutes(route.routes) }
    </div>
);

const ComplexComponentTabAll = () => (
    <div>
        <p> This is the ComplexComponentTabAll </p>
    </div>
);

我期待某种类型的tabing体系结构,通过使用/complex-route和/或/complex-route/:all,ComplexComponent将成为呈现ComplexeComponentAll的包装组件。

但是,ComplexComponentAll要么没有呈现,要么呈现两次(取决于我对路由的挠痒痒)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-11 22:05:49

问题是,命名参数路由/complex-route/:all (与/complex-route/以下的任何内容匹配)出现在/complex-route/tab-1/complex-route/tab-2路由之前。由于react-router-config在内部使用<Switch> (只显示第一个匹配),其他两条路由永远不会显示。

/complex-route/:all成为最后一种修复它的路由,但是使用带有正则表达式的精确路由只匹配/complex-route/complex-route/all是一个更干净的解决方案:

代码语言:javascript
复制
{
    path      : '/complex-route/(all)?',
    exact     : true,
    component : ComplexComponentTabAll,
}

有关path react-router-configreact-router的语法的更多信息,请查看 docs

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49202510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档