【发布时间】:2021-07-11 07:32:40
【问题描述】:
使用 Typescript 和 react-router-dom。我声明了一个数组以在Switch 中动态生成Route 子代。但是得到了类型错误:
Conversion of type '{ routeMap: RouteProps<string, { [x: string]: string | undefined; }>[]; "": any; }' to type 'Switch' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Type '{ routeMap: RouteProps<string, { [x: string]: string | undefined; }>[]; "": any; }' is missing the following properties from type 'Switch': context, setState, forceUpdate, render, and 3 more.
这是我的代码:
import { RouteProps, Switch, Redirect, Route } from 'react-router-dom'
export const routeMap: RouteProps[] = [
{
path: '/',
exact: true,
component: loadable(() => import('Views/Home')),
},
{
path: `${base}`,
exact: true,
component: loadable(() => import('Views/Home')),
},
{
path: `${base}/index`,
component: loadable(() => import('Views/Home')),
},
{
path: `${base}/404`,
component: loadable(() => import('Views/404')),
},
{
path: `${base}/floorConstruct`,
component: loadable(() => import('Views/FloorConstruct')),
},
{
path: `${base}/dataTracking`,
component: loadable(() => import('Views/DataTracking')),
},
{
path: `${base}/pageConfig`,
component: loadable(() => import('Views/PageConfig')),
},
]
export const StaticRouters = () => (
<Switch>
{routeMap.map((route, index) => (
// eslint-disable-next-line react/no-array-index-key
<Route key={index} {...route} />
))}
<Redirect to={`${base}/404`} />
</Switch>
)
我发现StaticRouter 是() => boolean ???
我已将部分代码上传到codesandbox。
【问题讨论】:
-
JSX 代码需要在 .tsx 文件中(如果你问我,这很烦人,但它就是这样)
标签: reactjs typescript react-router react-router-dom