react-router链接打字稿错误

时间:2017-06-07 18:50:22

标签: reactjs typescript ecmascript-6 react-router

我刚开始在我的React项目中添加TypeScript,我无法弄清楚这个错误:

import {Link} from 'react-router';
Module '"/Users/.../node_modules/@types/react-router/index"' has no exported member 'Link'.

Link中存在react-router组件(代码正常),但TypeScript无法识别它。我添加了@types/react-router,似乎没有实现Link。有什么想法吗?

摘自package.json

"@types/react": "^15.0.25",
"@types/react-dom": "^15.5.0",
"@types/react-router": "^4.0.11",
"@types/reactstrap": "^4.3.4",

...

"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-router": "^3.0.2",
"reactstrap": "^4.2.0"

...

"typescript": "^2.3.3",

1 个答案:

答案 0 :(得分:7)

你有react-router版本3,但版本4的@types包.refer-router版本4将很多库移动到react-router-dom包中,这就是为什么他们的类型不是在@types/react-router v4包中。

您应该删除不正确的类型包:

npm un @types/react-router

然后使用以下命令安装正确的类型:

npm i --save-dev @types/react-router@3