为什么我的链接在React-Router中不起作用?
试图为网站创建一个关于IM的网站的页面,我在堆栈上找到了此解决方案,但对我不起作用。我正在为原始代码使用过时的教程,这是我当前的代码:
about.js
:
import React from "react";
import { Link, Route, useMatch } from "react-router-dom";
import SinglePage from "./SinglePage";
const About = () => {
//const match = useMatch('/');
return (
<div className="about__content">
<ul className="about__list">
<li>
<Link to={'about-app'}>About App</Link>
</li>
<li>
<Link to={'about-author'}>About Author</Link>
</li>
</ul>
<Route path={':slug'}>
<SinglePage />
</Route>
</div>
);
};
export default About;
index.js
我正在呈现组件:
import React from "react";
import ReactDOM from "react-dom";
import TodoContainer from "./functionBased/components/TodoContainer"; // Component file
import "./functionBased/App.css"; // Style sheet
import { HashRouter as Router, Routes, Route } from "react-router-dom"; // Router file
import About from "./functionBased/pages/About";
import NotMatch from "./functionBased/pages/NotMatch";
ReactDOM.render(
<React.StrictMode>
<Router>
<Routes>
<Route exact path="/" element={<TodoContainer />} />
<Route path="/about/*" element={<About />} />
<Route path="*" element={<NotMatch />} />
</Routes>
</Router>
</React.StrictMode>,
document.getElementById("root")
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题
直接渲染
路由
组件。路由
组件只能由路由
组件或其他路由
组件作为嵌套路由。react-router-dom@6
路由
组件在element
prop上渲染其内容。解决方案
路由
组件,并包装douscentent路由
由`左右呈现的组件。元素
prop上渲染单页
。示例:
替代方案
,您可以将
单页移动
作为嵌套路由(而不是作为后裔的位置)移动到主路由器路线)。示例:
...
Issues
About
component is directly rendering aRoute
component. TheRoute
component can only be rendered by aRoutes
component or anotherRoute
component as a nested route.react-router-dom@6
Route
components render their content on theelement
prop.Solution
Routes
component and wrap the descendentRoute
component rendered by `About.SinglePage
on the route'selement
prop.Example:
Alternative
You could alternatively move the
SinglePage
route out to the main router as a nested route (instead of where it is as a descendent route).Example:
...
您正在用
/about/*
定义路由,并使用of tosemething
访问它们,该路由根本不存在,添加\ of of \ auter
在to
链接
中。You are defining the routes with
/about/*
and accessing them withabout-something
which does not exist at all, add\about\author
into
forLink
.