【发布时间】:2020-02-29 07:57:44
【问题描述】:
我的应用中有两个主要的页面“shell”。第一个 shell 用于“unauth”页面登录流程(背景图片、material-ui 纸),第二个 shell 是主仪表板(导航栏、侧边栏等)。
以下代码是我尝试简化遇到的问题。有人可以告诉我如何使用 react-router-dom 正确实现这一点吗?
<BrowserRouter>
<Switch>
<Route component={Shell1}>
<Route path="/test1" exact component={() => <div>Test 1</div>} />
<Route path="/test2" exact component={() => <div>Test 2</div>} />
</Route>
<Route component={Shell2}>
<Route path="/test3" exact component={() => <div>Test 3</div>} />
<Route path="/test4" exact component={() => <div>Test 4</div>} />
</Route>
</Switch>
</BrowserRouter>
我从another StackOverflow post 得到了这个尝试,但是上面的代码不起作用。导航到 /test1 时,Shell1(只是一个显示 Shell1 的 div)不显示,并且 /test3 + /test4 根本不起作用。
这是一个代码沙盒演示:https://codesandbox.io/s/react-example-362ow
提前致谢。
【问题讨论】:
-
你真的需要这个开关吗?
A <Switch> looks through its children <Route>s and renders the first one that matches the current URL. -
@artm 在没有开关的情况下进行了测试,仍然没有看到渲染的 Shell。我实际上认为没有开关,两个 shell 都可能被渲染,但事实并非如此。
标签: reactjs react-router react-router-dom