【问题标题】:reactjs - Is it possible to have non nested routesreactjs - 是否可以有非嵌套路由
【发布时间】:2015-07-16 22:39:20
【问题描述】:

我正在玩 reactjs 路由。在所有示例中,路由总是嵌套的,例如

<Route path="/" handler={App} >
    <Route name="about" handler={About} />
    <Route name="contact" handler={Contact} />
</Route> 

是否可以有简单的非嵌套路由,如下所示?

 <Route path="/" handler={App} />
 <Route name="about" handler={About} />
 <Route name="contact" handler={Contact} />

更新:

var routes = (
    <Route name="root" handler={Root}>
        <Route path="/" handler={Home} />
        <Route path="/home" handler={Home} />
        <Route path="/about" handler={About} />
        <Route path="/projects" handler={Projects} />
        <Route path="/contact" handler={Contact} />
    </Route> 
);

按照以下答案的建议更新到此之后,出现了奇怪的问题。 name 不再工作只有path 工作?我不得不更新我的路线。任何的想法?

【问题讨论】:

    标签: reactjs react-router


    【解决方案1】:

    似乎不可能。但我正在使用一个技巧来模仿这种行为:

    var Routes = (
        <Route name="root" handler={Root}>
            <Route name="checkout" path="/checkout/:step" handler={Checkout}/>
            <Route name="application" path="/" handler={Application}>
                <DefaultRoute handler={Promo}/>
                <Route name="agreement" handler={Agreement}/>
                <Route name="policy" handler={Policy}/>
                <Route name="how-it-works" handler={Brief}/>
                <Route name="login" handler={Login}/>
                <Route name="faq" handler={Faq}/>
                ...
                <NotFoundRoute handler={NotFound}/>
            </Route>
            <NotFoundRoute handler={NotFound}/>
        </Route>
    );
    

    Root 只是一个带有 body 标签的基本 html 页面,其内容由路由处理程序呈现。

    在这里,我需要一个具有不同页面布局但与协议或登录页面处于相同路径段级别的结帐页面。

    【讨论】:

    • 这个例子来自工作项目。处理的路由将是:/、/checkout/*、/agreement 等。代码大约是 2 个月前写的。
    猜你喜欢
    • 2016-08-31
    • 2019-08-13
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多