javascript < /Link>命令不会路由,React.Js

vybvopom  于 2023-11-15  发布在  Java
关注(0)|答案(1)|浏览(87)


的数据



首先,我为我糟糕的英语道歉。正如你在图片中看到的,当我按下登录按钮时,url部分发生了变化,但没有重定向到页面。

import React from "react";

import 'bootstrap/dist/css/bootstrap.min.css';
import './App.css'
import logo from "./assets/resim.png"
import { BrowserRouter, Route, Routes, Link } from 'react-router-dom';
import Forgot from './App_sifre.jsx'
import Home from './App_anasayfa.jsx'

function App() { 
  return (    
     <>
       <BrowserRouter>
       <Navbar/>
         <Routes>
          <Route path='/' element= {<Home />} />
          <Route path='/forgot' element= {<Forgot />} />
         </Routes>
      </BrowserRouter>
     </>
)}

export default function LoginPage () {
  return (
    <BrowserRouter>
    <div className="bg">
       <div className="container" >
         <div className="card card-body mt-5 pb-5 shadow p-3 mb-5 bg-body rounded ">
           <h2 className="text-center text-info ">DMS Planlama Sistemi</h2>
           <div className="row mt-5 pb-5">
             <div className="col-lg-6 col-sm-12 mb-5">
               <img src={logo} alt="login" className="img-fluid" />
             </div>
             <div className="col-lg-6 col-sm-12">
               <form>
                 <div className="form-group">
                   <label htmlFor="email">Kullanıcı Adı:</label>
                   <input type="email" className="form-control" placeholder="Kullanıcı adınız" id="email" />
                 </div>
                 <div className="form-group">
                   <label htmlFor="pwd" style={{marginTop: '15px'}}>Şifre:</label>
                   <input type="password" className="form-control" placeholder="şifreniz" id="pwd" />
                 </div>
                 <div className="form-group form-check">
                   <label className="form-check-label">
                     <input className="form-check-input" type="checkbox" /> Beni Hatırla
                     <label><a href="şifremi unuttum/şifre yenileme.html" to="/sifre" style={{marginLeft: '30px'}}>Şifremi unuttum</a>
                     </label>
                   </label>
                 </div>
                   <button type="button" className="btn btn-primary">
                   <Link to="/forgot" >Login</Link>
                   </button>
               </form>
             </div>
           </div> 
         </div>
       </div>
       </div>
       </BrowserRouter>
 )
 }

字符串
我检查了我导入的react文件,但它们都在同一个地方,不可能有错误。我应该怎么做来修复它?


xpcnnkqh

xpcnnkqh1#

在标签中使用标签可能无法正常工作,因为标签有自己的行为来处理点击事件。相反,您可以使用react-router-dom中的useHistory钩子在按钮被点击时进行导航。

import { BrowserRouter, Link, useHistory } from "react-router-dom";
const history = useHistory();
  const handleLoginClick = () => {

    history.push("/forgot");
  };

  <button
     type="button"
     className="btn btn-primary"
     onClick={handleLoginClick}
     >
      Login
      </button>

字符串

相关问题