类型不存在属性';Exact'; [英] Property 'exact' does not exist on type
本文介绍了类型不存在属性';Exact';的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正尝试在我的Reaction应用程序中使用react-router-dom
,并且我也在使用TypeScrip而不是javascript。这里的问题是,我不能在我的组件中导入Routing并使其工作。我已经安装了@types/react-router-dom
,但由于某种原因,它仍然无法按预期工作。
这是试图使用react-router-dom
import {BrowserRouter as Router, Route} from "react-router-dom";
const App = () => {
return (
<div>
<Router>
<div>
<Route path="/" exact/>
</div>
</Router>
</div>
)
}
export default App;
这就是我收到的错误
TypeScript error in /Users/veselinkontic/Projects/givellet/frontend/src/components/index.tsx(9,37):
Type '{ path: string; exact: true; }' is not assignable to type 'IntrinsicAttributes & (PathRouteProps | LayoutRouteProps | IndexRouteProps)'.
Property 'exact' does not exist on type 'IntrinsicAttributes & (PathRouteProps | LayoutRouteProps | IndexRouteProps)'. TS2322
7 | <Router>
8 | <div>
> 9 | <Route path="/" exact/>
| ^
10 | </div>
11 | </Router>
12 | </div>
这是我的Package.json文件,您可以在其中看到所有内容。
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/node": "^12.0.0",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@types/react-router-dom": "^5.3.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^6.0.1",
"react-scripts": "4.0.3",
"typescript": "^4.1.2",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
推荐答案
Reaction路由器v6不再支持exact
。
//旧-v5
<Route exact path="/" component={Home} />
//新-V6
<Route path="/" element={<Home />} />
如他们的文档所述:
您不再需要在
<Route path="/">
上使用精确的道具。 这是因为默认情况下所有路径都完全匹配。如果你 我想匹配更多的URL,因为您的子路由使用尾随*AS 在<Route path="users/*">
中。
您可以参考此迁移指南:https://reactrouter.com/docs/en/v6/upgrading/v5
这篇关于类型不存在属性';Exact';的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文