【问题标题】:Reactjs Static and Dynamic RoutesReactjs 静态和动态路由
【发布时间】:2021-05-16 09:55:29
【问题描述】:

我正在尝试在我的 React 应用程序中同时使用静态路由和动态路由。我有一个 JSON 文件,其中包含应用程序具有的所有动态路由,如下所示:

[
  {
    "Text": "Home",
    "Link": "/"
  },
  {
    "Text": "Users",
    "Link": "/Users"
  },
  {
    "Text": "Posts",
    "Link": "/Posts"
  }
]

这是我的应用程序(我删除了一些不需要的代码)

import React from 'react';

function App() {

    return (
        <div className="Grid-Container">
           <Router>
            <Switch>
                <div>
                    <!-- Static Route to the Settings section -->
                    <Route
                        path='/settings/home'
                        component={Settings}
                        key={'Settings'}/>
                    <!-- Dynamic Route which displays all Entries for the chosen Page -->
                    <Route
                        path={`/${page.Text}`}
                        component={objectPage}
                        key={`${page.Text}-List`}
                        exact/>
                    <!-- Dynamic Route which displays one Entry for the chosen Page --> 
                    <Route
                        path={`/:object/:id`}
                        component={DetailPage}
                        key={`Detail-Route`}
                        exact/>
                </div>
            </Switch>
          </Router>
        </div>
    );
}

export default App;

动态路线按预期工作,但是当我尝试转到设置页面时,我看到了我不想要的详细信息页面的内容。

【问题讨论】:

    标签: reactjs react-router


    【解决方案1】:

    尝试将静态 Route 作为 Switch 的第一个子级。

    正在发生的事情是Switch 正在按时间顺序将您的 URL 与您的路由进行比较。它将您的settings 解释为url 参数:object。因为它现在找到了一个它认为与您的 URL 匹配的 Route,所以它会呈现您的详细信息页面,并且不会检查您的任何其他 Routes 是否也匹配甚至更好。

    import React from 'react';
    
    function App() {
    
        return (
            <div className="Grid-Container">
                <Router>
                    <Switch>
                        <!-- Static Route to the Settings section -->
                        <Route
                            path='/settings'
                            component={Settings}
                            key={'Settings'}/>
    
                        <!-- Dynamic Route which displays all Entries for the chosen Page -->
                        <Route
                            path={`/${page.Text}`}
                            component={<ObjectPage />}
                            key={`${page.Text}-List`}
                            exact/>
                        <!-- Dynamic Route which displays one Entry for the chosen Page -->
                        <Route
                            path={`/:object/:id`}
                            component={DetailPage}
                            key={`Detail-Route`}
                            exact/>
                    </Switch>
                </Router>
            </div>
        );
    }
    
    export default App;
    

    【讨论】:

    • 我已经按照您的描述更改了订单。但现在,我有另一种方式。首先,显示设置组件,并在其下方显示详细信息页面。
    • “下”是什么意思,通常&lt;Switch&gt; 只会渲染第一个满足path&lt;Route&gt;。您是否将&lt;Route&gt; 放在&lt;Switch&gt; 之外?
    • Ehmm,您在更新后的帖子中将所有 Routes 包装在 div 中。可能是导致问题的原因
    • 谢谢,这就是问题所在。
    【解决方案2】:

    使用精确的道具

    <Route
           path='/settings'
           component={Settings}
           key={'Settings'}
           exact
    />
    

    【讨论】:

      猜你喜欢
      • 2016-06-08
      • 1970-01-01
      • 2022-01-04
      • 2018-12-30
      • 2022-07-11
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多