首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >响应路由器链接不能跳转到页面

响应路由器链接不能跳转到页面
EN

Stack Overflow用户
提问于 2022-05-03 13:23:44
回答 1查看 593关注 0票数 0

我在我的项目中使用了react-router-dom .这是我的代码:

App.js

代码语言:javascript
复制
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';

function App() {
  const currentUser = true;

  return (
    <Router>
      <Topbar />
      <Switch>
        <Route exact path="/">
          <Home />
        </Route>
        <Route path="/register">
          {currentUser ? <Home /> : <Register />}
        </Route>
        <Route path="/login">{currentUser ? <Home /> : <Login />}</Route>
        <Route path="/post/:id">
          <Single />
        </Route>
        <Route path="/write">{currentUser ? <Write /> : <Login />}</Route>
        <Route path="/settings">
          {currentUser ? <Settings /> : <Login />}
        </Route>
      </Switch>
    </Router>
  );
}

Topbar.jsx:

代码语言:javascript
复制
function Topbar() {
  const user = true;
  return (
    <div className="top">
        <div className="topLeft">
            <span className="topIcon iconfont icon-weixin"></span>
            <span className="topIcon iconfont icon-weibo"></span>
            <span className="topIcon iconfont icon-douyin"></span>
        </div>
        <div className="topCenter">
            <ul className="topList">
                <li className="topListItem"><Link className="link" to="/">HOME</Link></li>
                <li className="topListItem"><Link className="link" to="/">ABOUT</Link></li>
                <li className="topListItem"><Link className="link" to="/">CONTACT</Link></li>
                <li className="topListItem"><Link className="link" to="/write">WRITE</Link></li>
                <li className="topListItem">
                    { user && "LOGOUT" }
                </li>
            </ul>
        </div>
        <div className="topRight">
            {
                user ? (
                    <img className="topImg" src="https://assets.imgix.net/hp/snowshoe.jpg?auto=compress&w=900&h=600&fit=crop" alt="profile" />
                ) : (
                    <ul className="topList">
                        <li className="topListItem"><Link className="link" to="/login">LOGIN</Link></li>
                        <li className="topListItem"><Link className="link" to="/register">REGISTER</Link></li>
                    </ul>
                )
            }
            
            <span className="topSearchIcon iconfont icon-sousuo"></span>
        </div>
    </div>
  )
}

当我单击<Link /><Write/>组件时,URL会更改,但页面不会更改.有人知道为什么吗?许多人谈到exact属性,但我已经在<Home/>组件中添加了这一点。

EN

回答 1

Stack Overflow用户

发布于 2022-05-03 14:36:04

我找到了解决办法..。这是版本问题,我使用react18和反应性路由器-dom v5.而我把反应路由器变成v6,问题就解决了!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72100073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档