react.js實(shí)現(xiàn)頁面登錄跳轉(zhuǎn)示例
1,頁面目錄信息:

2,從index.js導(dǎo)入路由信息BasicRoute.js,然后BasicRoute.js中存儲(chǔ)App.js和StatisticsInformation.js頁面信息,從App.js登錄成功后跳轉(zhuǎn)到StatisticsInformation.js頁面。
3,index.js略
4,BasicRoute.js
import React from 'react';
import {HashRouter, Route, Switch,hashHistory} from 'react-router-dom'; //導(dǎo)入react-router-dom組件
import App from '../App'; //導(dǎo)入頁面
import StatisticsInformation from '../firstpage/StatisticsInformation'; //導(dǎo)入頁面
import createBrowserHistory from "history/createBrowserHistory"; //導(dǎo)入history包
const customHistory = createBrowserHistory(); //創(chuàng)建
const BasicRoute = () => (
<HashRouter history={customHistory}> //給路由添加屬性history,這樣父組件就可以調(diào)用this.props.history.push
<Switch>
<Route exact path="/" component={App}/> //注冊組件
<Route exact path="/firstpage/StatisticsInformation" component={StatisticsInformation}/> //注冊組件
</Switch>
</HashRouter>
);
export default BasicRoute;
5, App.js頁面:
export ?default ?class ?App ?extends ? React.Component{
?render(){
? ? ? ? return (
? ? ? ? ? ? <div className="back">
? ? ? ? ? ? ? <Login ?history={this.props.history} url='http://www.baidu.com' /> ?//將this.props.history作為屬性傳遞給子組件Login。因?yàn)樽咏M件不具備history屬性。
? ? ? ? ? ? </div>
? ? ? ? );}}6,Login.js頁面進(jìn)行登錄驗(yàn)證,驗(yàn)證函數(shù)也在這里,實(shí)現(xiàn)校驗(yàn)成功后跳轉(zhuǎn):
class Login ? extends ? React.Component{
? ? ?submitFun(username,password){ ? ? ? ? ? ?//登錄驗(yàn)證函數(shù) ? ? ? ? ?
? var ?newpage = this.props.newpage; ? ? ? ? ?//跳轉(zhuǎn)的目標(biāo)頁面
?this.props.history.push(newpage); ? ? ? ? ?//實(shí)現(xiàn)跳轉(zhuǎn)
? ? ? ? ?} ; ?
?render(){ ? ? ??
? ? ? return(
? ? ? ? ?<Form ?onSubmit={(username,password)=>this.submitFun(username,password)} > ? //登錄的時(shí)候觸發(fā)函數(shù)
? ? ? ? </Form>
? ? ? ?) }
}7,哦,別忘了:
npm ?install ?react-router-dom npm ?intall ? history
到此這篇關(guān)于react.js實(shí)現(xiàn)頁面登錄跳轉(zhuǎn)示例的文章就介紹到這了,更多相關(guān)react.js登錄跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React中使用dnd-kit實(shí)現(xiàn)拖曳排序功能
在這篇文章中,我將帶著大家一起探究React中使用dnd-kit實(shí)現(xiàn)拖曳排序功能,由于前陣子需要在開發(fā) Picals 的時(shí)候,需要實(shí)現(xiàn)一些拖動(dòng)排序的功能,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
解決React報(bào)錯(cuò)Unexpected default export of an
這篇文章主要為大家介紹了React報(bào)錯(cuò)Unexpected default export of anonymous function解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
react-native動(dòng)態(tài)切換tab組件的方法
在APP中免不了要使用tab組件,有的是tab切換,也有的是tab分類切換.這篇文章主要介紹了react-native動(dòng)態(tài)切換tab組件的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-07-07
React實(shí)現(xiàn)數(shù)字滾動(dòng)組件numbers-scroll的示例詳解
數(shù)字滾動(dòng)組件,也可以叫數(shù)字輪播組件,這個(gè)名字一聽就是非常普通常見的組件。本文將利用React實(shí)現(xiàn)這一組件,感興趣的小伙伴可以了解一下2023-03-03

