打字稿和反应路由器

Typescript and react-router

尝试起床并运行在 typescript 上进行 react-router。我已经安装了@types/react-router,但是我遇到了一个错误,上面写着:

@types/react-router/index"' has no exported member 'hashHistory'

我一直在尝试不同版本的路由器和类型。我当前的版本是:

react:  16.3.2
react-router: 4.2.0
@types/react-router: 4.0.23

我查看了 github 上的打字项目。 index.ds.ts 不显示正在导出的 hashHistory。但是,有一个 v3 文件夹,那里有 index.ds.ts。

我希望我刚刚找到了所有相关包的神奇版本组合。

在 react-router 4 中,web、native 和 core 有单独的包。 您需要安装 react-router-dom 而不是 react-router (docs)

此外,

With React Router v4, one of the big changes is that there are a number of different router components. Each one will create a history object for you. The <BrowserRouter> creates a browser history, the <HashRouter> creates a hash history...

因此,您可以只使用 HashRouter,而不是将历史对象传递给路由器。

import { HashRouter as Router } from 'react-router-dom'

Documentation on HashRouter