简体   繁体   English

打字稿和反应路由器

[英]Typescript and react-router

trying to get up and running with react-router on typescript. 尝试在打字稿上启动并运行react-router。 I have installed @types/react-router, but I am having an issue with an error that reads: 我已经安装了@ types / react-router,但是我遇到了一个错误消息:

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

I have been trying different versions of router and the types. 我一直在尝试不同版本的路由器和类型。 My current versions are: 我当前的版本是:

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

I have looked at the typings project on github . 我看过github上的打字项目。 The index.ds.ts does not show hashHistory being exported. index.ds.ts不显示正在导出的hashHistory However, there is a v3 folder, and the index.ds.ts there does. 但是,有一个v3文件夹,并且那里有index.ds.ts。

I'm hoping that I just having found that magical mixture of versions for all involved packages. 我希望我刚刚发现了所有涉及的软件包的神奇混合版本。

In react-router 4, there are separate packages for web, native and core. 在react-router 4中,有针对Web,本机和核心的单独软件包。 You need to install react-router-dom instead of react-router ( docs ) 您需要安装react-router-dom而不是react-routerdocs

Also, 也,

With React Router v4, one of the big changes is that there are a number of different router components. 使用React Router v4,最大的变化之一是有许多不同的路由器组件。 Each one will create a history object for you. 每个人都会为您创建一个历史对象。 The <BrowserRouter> creates a browser history, the <HashRouter> creates a hash history... <BrowserRouter>创建浏览器历史记录, <HashRouter>创建哈希历史记录...

So instead of passing a history object to the Router, you can just use the HashRouter. 因此,您不必使用历史记录对象到路由器,而只需使用HashRouter。

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

Documentation on HashRouter HashRouter上的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM