简体   繁体   English

类型上不存在“精确”属性

[英]Property 'exact' does not exist on type

I am trying to use react-router-dom inside my react app and also I am using typescript instead of javascript. The issue here is that I can't import Route inside my component and make it work.我正在尝试在我的 React 应用程序中使用react-router-dom ,并且我正在使用 typescript 而不是 javascript。这里的问题是我无法在我的组件中导入 Route 并使其工作。 I already installed @types/react-router-dom but for some reason it's still not working as expected.我已经安装了@types/react-router-dom但由于某种原因它仍然没有按预期工作。

This is a component that is trying to use 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;

And this is the error that I am getting这是我得到的错误

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>

And this is my package.json file in which you can see that everything is there.这是我的 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"
    ]
  }
}

react router v6 doesn't support exact anymore.反应路由器 v6 不再支持exact

// old - v5 <Route exact path="/" component={Home} /> // 旧 - v5 <Route exact path="/" component={Home} />

// new - v6 <Route path="/" element={<Home />} /> // 新 - v6 <Route path="/" element={<Home />} />

As stated in their documentation:如他们的文档中所述:

You don't need to use an exact prop on <Route path="/"> anymore.您不再需要在<Route path="/">上使用确切的道具。 This is because all paths match exactly by default.这是因为默认情况下所有路径都完全匹配。 If you want to match more of the URL because you have child routes use a trailing * as in <Route path="users/*"> .如果您想匹配更多 URL,因为您有子路由,请使用尾随 *,如<Route path="users/*">

You can refer to this migration guide: https://reactrouter.com/docs/en/v6/upgrading/v5你可以参考这个迁移指南: https ://reactrouter.com/docs/en/v6/upgrading/v5

I have been through this same issue, the new React-Router doesn't support the exact keyword.我也遇到过同样的问题,新的 React-Router 不支持exact关键字。 You can simply remove it from the <Route .../> and it will work just fine.您可以简单地将其从 <Route .../> 中删除,它会正常工作。

Also instead of component you have to use element and pass the element tag into it.此外,您必须使用element并将元素标签传递给它,而不是component

I don't know if this keeps happening to you.我不知道这种情况是否经常发生在你身上。

But there you have:但是你有:

"dependencies": {
  ...
  "@types/react-router-dom": "^5.3.2",
  ...
  "react-router-dom": "^6.0.1",
  ...
},

And maybe the problem is that the version of your react-router-dom and the types are not the same and give compatibility problems.也许问题是您的 react-router-dom 版本和类型不一样,并且会出现兼容性问题。 This library has not been updated yet.此库尚未更新。 The same thing happened to me with a project that I just started.我刚开始的一个项目也发生了同样的事情。

To solve this problem, you can downgrade the version of your react-router-dom to v5 and work under that syntax, or wait for the update of the types and use the most recent version.要解决此问题,您可以将 react-router-dom 的版本降级到 v5 并使用该语法,或者等待类型更新并使用最新版本。 Keep in mind that there are important changes in v6 and updating when you have a lot of code could be complicated.请记住,v6 中有重要更改,当您有大量代码时更新可能会很复杂。

Likewise, the previous answers are correct, exact does not exist in the new v6 of react-router .同样,之前的答案是正确的,在react-router的新 v6 中不存在exact的答案。

Here you can see the current version of the types: https://www.npmjs.com/package/@types/react-router-dom在这里你可以看到类型的当前版本: https ://www.npmjs.com/package/@types/react-router-dom

In the case of React Router v6, I add Routes and Route to the import.对于 React Router v6,我将 Routes 和 Route 添加到导入中。

Example:例子:

import { BrowserRouter, Route, Routes  } from 'react-router-dom';
import  Home  from "./pages/Home";
import  NewRoom  from "./pages/NewRoom";
    
function App() {
  return (
  <BrowserRouter>
      <Routes>
      <Route path="/"  element={<Home />}/>
      <Route path="/rooms/new" element={<NewRoom />}/>
      </Routes>
  </BrowserRouter>
  );
}
    
export default App;

what worked for me was re-installing the package npm install react-router-dom对我有用的是重新安装 package npm install react-router-dom

Just replace exact with end .只需将exact替换为end

Rename <NavLink exact> to <NavLink end> .<NavLink exact>重命名为<NavLink end> https://reactrouter.com/en/v6.3.0/upgrading/v5#rename-navlink-exact-to-navlink-end https://reactrouter.com/en/v6.3.0/upgrading/v5#rename-navlink-exact-to-navlink-end

I found the solution in React-Router docs...我在 React-Router 文档中找到了解决方案... 在此处输入图片说明

Here is the link to this page section: https://v5.reactrouter.com/web/api/NavLink/style-object-func这是此页面部分的链接: https : //v5.reactrouter.com/web/api/NavLink/style-object-func

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

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