简体   繁体   English

使用react-router-dom

[英]Using react-router-dom

I split my routes into two files. 我将路线分为两个文件。

const masterRoutes=(

                      <Switch>
                      <Route path="/diamond"  component={DiamondMaster}   />
                      <Route path="/gram"     component={GramMaster}      />
                      </Switch>

                    )

export default masterRoutes; 



const userRoutes=(

                      <Switch>
                      <Route path="/user"  component={User}   />
                      <Route path="/store" component={Store} />
                      <Route path="/userlist" component={UserList} />
                      </Switch>

)



export default userRoutes;

app.jsx is the main file handling all routes. app.jsx是处理所有路由的主要文件。

export class App extends Component {
  render() {
    return (

          <div>


                  <Router history={history}  >
                      <div>
                      <AuthHeader isLoggedIn={true} />
                      <Route path="/" component={AuthHeader} />                                 
                      <Switch>
                      <Route path="/login" component={Login} />
                      <Route path="/tab" component={SettingsComp} />
                       {userRoutes}
                       {masterRoutes}
                     </Switch>
                      </div>
                  </Router>
            </div>
    );
  }

It is working fine when i click user component But MasterRoutes is not working. 当我单击用户组件时,它工作正常,但MasterRoutes无法正常工作。

App.jsx file doesn't take MasterRoutes file. App.jsx文件不使用MasterRoutes文件。 Is there any other way to implement this? 还有其他方法可以实现吗?

I am bit new to reactjs. 我对reactjs有点陌生。 Please bear with my question. 请忍受我的问题。

What is happening is the first Switch statement is taking precedence. 发生的情况是第一个Switch语句具有优先权。 If you swap them around, then master works, and user doesn't. 如果交换它们,则master起作用,而用户则不起作用。

I'm not exactly sure what the goal is but if you want the list of routes to be extracted, you can instead do this: 我不确定目标是什么,但是如果要提取路由列表,则可以执行以下操作:

const masterRoutes=[
  <Route path="/diamond"  component={DiamondMaster}   />
  <Route path="/gram"     component={GramMaster}      />
];

const userRoutes=[
  <Route path="/user"  component={User}   />
  <Route path="/store" component={Store} />
  <Route path="/userlist" component={UserList} />
];

Instead of each of them having their own Switch , they instead are just arrays of Route s. 他们不是每个人都有自己的Switch ,而是只是Route的数组。 So when you add them to the Switch tag you already have, you are just extending the number of routes to include those from userRoutes and masterRoutes. 因此,将它们添加到已经拥有的Switch标记中时,您只是在扩展路由的数量以包括来自userRoutes和masterRoutes的路由。

All of your other code should be able to stay the same. 您的所有其他代码都应该保持不变。 Exporting, importing and adding them to the router {masterRoutes} {userRoutes} . 将它们导出,导入并添加到路由器{masterRoutes} {userRoutes}

If you want child routes, that would handled differently. 如果您想要子路线,则处理方式会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM