【发布时间】:2017-04-16 11:56:36
【问题描述】:
我正在尝试将我的大部分路线呈现为 AppShell 组件的子组件,该组件包含一个导航栏。但是我想将我的 404 路由呈现为一个独立的组件,而不是包装在 AppShell 中。
使用 v2 很容易:
<Router>
<Route component={AppShell}>
<Route path="/about" component={About} />
<Route path="/" component={Home} />
</Route>
<Route path="*" component={NotFound} />
</Router>
一切正常:
-
/渲染<AppShell><Home /></AppShell> -
/about渲染<AppShell><About /></AppShell> -
/blah渲染<NotFound />
但我不知道如何使用 v4:
现在我正在这样做,但问题是它呈现AppShell(没有孩子,但仍然是导航栏):
const Routes = () => (
<div>
<AppShell>
<Match exactly pattern="/" component={Home} />
<Match pattern="/about" component={About} />
</AppShell>
<Miss component={NotFound} />
</div>
)
有了这个:
-
/渲染<div><AppShell><Home /></AppShell></div>(好) -
/about渲染<div><AppShell><About /></AppShell></div>(好) -
/blah渲染<div><AppShell /><NotFound /></div>(问题——我想摆脱<AppShell />)
如果没有根路由,则使用数组 pattern 有效:
const InAppShell = (): React.Element<any> => (
<AppShell>
<Match pattern="/about" component={About} />
<Match pattern="/contact" component={Contact} />
</AppShell>
)
const App = (): React.Element<any> => (
<div>
<Match pattern={['/contact', '/about']} component={InAppShell} />
<Miss component={NotFound} />
</div>
)
使用pattern 和exactly 的数组与根路由一起使用:
但是我必须将所有可能的子路由放在pattern数组中...
const InAppShell = (): React.Element<any> => (
<AppShell>
<Match exactly pattern="/" component={Home} />
<Match pattern="/about" component={About} />
</AppShell>
)
const App = (): React.Element<any> => (
<div>
<Match exactly pattern={["/", "/about"]} component={InAppShell} />
<Miss component={NotFound} />
</div>
)
但在一个包含大量路由的大型应用程序中,这将是相当笨拙的。
我可以为/ 单独创建一个Match:
const InAppShell = (): React.Element<any> => (
<AppShell>
<Match exactly pattern="/" component={Home} />
<Match pattern="/about" component={About} />
<Match pattern="/contact" component={Contact} />
</AppShell>
)
const App = (): React.Element<any> => (
<div>
<Match exactly pattern="/" component={InAppShell} />
<Match pattern={["/about", "/contact"]} component={InAppShell} />
<Miss component={NotFound} />
</div>
)
但是,每次我往返于家乡路线时,这都会重新安装<AppShell>。
这里似乎没有理想的解决方案;我认为这是 v4 需要解决的基本 API 设计挑战。
如果我能做类似<Match exactlyPattern="/" pattern={["/about", "/contact"]} component={InAppShell} />...
【问题讨论】:
-
你解决了吗?我有完全相同的问题。
标签: http-status-code-404 react-router nested-routes