【发布时间】:2017-12-08 01:32:33
【问题描述】:
如何使用React Router 4 和静态路由定义 notFound 组件?
我使用react-router-redux 的next版本。所以,在我的客户中:
export default function appRoutes(history) {
return (
<ConnectedRouter history={history}>
<div>
{routes.map((route, key) => (
<Route key={key} {...route} />
))}
</div>
</ConnectedRouter>
);
}
路线是:
export const routes = [
{
path: '/',
component: App
},
{
path: '/:lang/chat',
component: Chat
},
{
path: '/:lang',
component: Listing
},
...
{
path: '*',
component: NotFound,
status: 404
},
];
使用这种方法,找不到的组件总是与匹配的路由一起显示。
我已经读过,我必须使用 Switch,而不是使用 div 包装上述 appRoutes 方法。但是使用这种方法,路由永远不会匹配。
编辑
我想同时显示,例如App组件和Listing组件,但NotFound也在显示。
那么,我做错了什么?
【问题讨论】:
标签: reactjs react-router react-router-redux react-router-v4