反应路由器路由器不显示
React router routers not showing
我似乎无法弄清楚为什么我的路由器路由没有呈现。我刚开始构建我的应用程序,所以没有太多代码,我尝试了几种不同的方法,但仍然一无所获。我现在在尝试路由的组件中只有 div,但里面的文本没有出现 - 当我在我的 App.js 文件中只有组件时,它们正在工作尝试路由它们。有什么想法吗?
import React from 'react';
import './App.css';
import { BrowserRouter as Router, Routes, Route} from 'react-
router-dom'
import Main from './components/Main';
import AddUser from './components/AddUser';
import EditUser from './components/EditUser';
function App() {
return (
<div className="App">
<header className="App-header">
<h1>Home</h1>
</header>
<Router>
<Routes>
<Route exact path="/" component={Main} />
<Route path="/AddUser" component={AddUser} />
<Route path="/edit" component={EditUser} />
</Routes>
</Router>
</div>
);
}
export default App;
// 我的 JSON 文件 \
{
"name": "w15-crud",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"bootstrap": "^5.1.3",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-router": "^6.3.0",
"react-router-dom": "^6.3.0",
"react-scripts": "^2.1.3",
"reactstrap": "^9.0.2",
"web-vitals": "^2.1.4"
},
"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-dom@6
Route
组件 API 已更改。不再有用于渲染路由组件的 component
道具。使用 element
属性并传递一个 ReactNode,a.k.a。 JSX.
declare function Route(
props: RouteProps
): React.ReactElement | null;
interface RouteProps {
caseSensitive?: boolean;
children?: React.ReactNode;
element?: React.ReactNode | null;
index?: boolean;
path?: string;
}
示例:
<Router>
<Routes>
<Route path="/" element={<Main />} />
<Route path="/AddUser" element={<AddUser />} />
<Route path="/edit" element={<EditUser />} />
</Routes>
</Router>
我似乎无法弄清楚为什么我的路由器路由没有呈现。我刚开始构建我的应用程序,所以没有太多代码,我尝试了几种不同的方法,但仍然一无所获。我现在在尝试路由的组件中只有 div,但里面的文本没有出现 - 当我在我的 App.js 文件中只有组件时,它们正在工作尝试路由它们。有什么想法吗?
import React from 'react';
import './App.css';
import { BrowserRouter as Router, Routes, Route} from 'react-
router-dom'
import Main from './components/Main';
import AddUser from './components/AddUser';
import EditUser from './components/EditUser';
function App() {
return (
<div className="App">
<header className="App-header">
<h1>Home</h1>
</header>
<Router>
<Routes>
<Route exact path="/" component={Main} />
<Route path="/AddUser" component={AddUser} />
<Route path="/edit" component={EditUser} />
</Routes>
</Router>
</div>
);
}
export default App;
// 我的 JSON 文件 \
{
"name": "w15-crud",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"bootstrap": "^5.1.3",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-router": "^6.3.0",
"react-router-dom": "^6.3.0",
"react-scripts": "^2.1.3",
"reactstrap": "^9.0.2",
"web-vitals": "^2.1.4"
},
"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-dom@6
Route
组件 API 已更改。不再有用于渲染路由组件的 component
道具。使用 element
属性并传递一个 ReactNode,a.k.a。 JSX.
declare function Route( props: RouteProps ): React.ReactElement | null; interface RouteProps { caseSensitive?: boolean; children?: React.ReactNode; element?: React.ReactNode | null; index?: boolean; path?: string; }
示例:
<Router>
<Routes>
<Route path="/" element={<Main />} />
<Route path="/AddUser" element={<AddUser />} />
<Route path="/edit" element={<EditUser />} />
</Routes>
</Router>