




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
Router使用及原Routerreact-router資react-react-react-用)或react-router-native(在rn中使用)。react-dom和actouer-nae都依賴ectoute,所以在安裝時(shí),ectroute也會(huì)自動(dòng)安裝,創(chuàng)建eb安npmnpminstall--savereact-router-創(chuàng)建importReact,{Component}from"react";import{BrowserRouter,Link,Route}fromimportHomePagefrom"./HomePage";importUserPagefromexportdefaultclassRouterPageextendsComponent{{return<Linkto="/">首頁<Linkto="/user">用戶中心{/*根路由要添加exact,實(shí)現(xiàn)精確匹配<Route<Routeexactpath="/"{HomePage}<Routepath="/user"{UserPage}}}<Route<Routepath="/search/:id"component={Search}添加導(dǎo)航<Link<Linkto={"/search/searchId}>搜索創(chuàng)建Search組件并獲取參數(shù)importimportReact,{Component}from"react";import{BrowserRouter,Link,Route}fromimportHomePagefromimportUserPagefromfunctionSearch({match,history,location{const{id}=match.params;return(<h1>Search:}exportdefaultclassRouterPageextendsComponent{render()constsearchId="1234";return(<Linkto="/">首頁<Linkto="/user">用戶中心<Linkto={"/search/searchId}>索
{/*根路由要添加exact,實(shí)現(xiàn)精確匹配<Routeexactpath="/"{HomePage}<Route<Routepath="/user"{UserPage}<Routepath="/search/:id"{Search}}}嵌functionfunction{return}functionSearch({match,history,location{const{id}=match.params;return(<h1>Search:<Linkto="/search/add">新增<Linkto={"/search/detail/id}><Routepath="/search/add"=><h1>add</h1>}<Routepath={"/search/detail/:"+id}component={Detail}/>}404{{/*添加Switch表示僅匹配一個(gè){/*根路由要添加exact,實(shí)現(xiàn)精確匹配<Routeexactpath="/"component={HomePage}<Routepath="/user"component={UserPage}<Routepath="/search/:id"<Routecomponent={()=><h1>404</h1>}importReact,{Component}from"react";import{Route,Redirect}from"react-router-import{connect}from"react-classPrivateRouteextends{render()const{path,component,isLogin}=if(isLogin)return<Routepath={path}{component}}{returnpathname:"/login",state:{redirect:path},}}}}exportdefaultconnect(state=>創(chuàng)建importReact,{Component}from"react";import{Redirect}from"react-router-dom";import{connect}from"react-redux";classLoginPageextends{render()const{isLogin,login,location}=const{redirect="/"}=||if(isLogin)return<Redirectto={redirect}}return<button}}exportdefaultexportdefaultconnect(state=>{login:()=>type:<Route<Routeexactpath="/login"{LoginPage}<PrivateRoutepath="/user"importimport{createStore,combineReducers}constinitalUserInfo{isLogin:user:name: functionloginReducer(state=...initalUserInfo},action)switch{casereturn{...initalUserInfo};case"loginSuccess":return{...state,isLogin:true};case"loginFailure":return{...state,isLogin:true};return{...state}}conststorecreateStore(combineReducers({user:loginReducer,exportdefaultimportReactfrom"react";importReactDOMfrom"react-dom";import"./index.css";importAppfromimport{Provider}from"react-redux";importstorefrom"./store";<Provider<App importimportReact,{Component}from"react";import{connect}from"react-redux";classUserPageextends{render()const{logout}=ps;return(<buttononClick={logout}>退出登}}}exportdefaultconnect(state=>{logout:()=>type:與HashRouterHashRouter最簡(jiǎn)單,不需要服務(wù)?端渲染,靠瀏覽?的#的來區(qū)分path就可以,BrowserRouter需要服務(wù)?端對(duì)不BrowserRouter使用HTML5歷史API(pushState,receState和popstate),讓頁面的UI同步HsRuter不支持ocaony和ocaonsae,動(dòng)態(tài)路由跳轉(zhuǎn)需要通過?傳遞參數(shù)。Hashhistory不需要服務(wù)?任何配置就可以運(yùn)行,如果你剛剛,那就使用它吧。但是我們不推薦在實(shí)際線上環(huán)境中用到它,因?yàn)槊恳粋€(gè)web應(yīng)用都應(yīng)該渴望使用拓BrowserRouter、Route、實(shí)現(xiàn)創(chuàng)建測(cè)試頁面importReact,{Component}from"react";import{BrowserRouter,Link,Route}fromimportHomePagefromimportUserPagefromexportdefaultclassMyRouterPageextendsComponent{{(<Linkto="/">首頁<Linkto="/user">用戶中心<Route<Routepath="/"exact{HomePage}<Routepath="/user"{UserPage}}}import{createBrowserHistory}fromconstRouterContext=React.createContext();classBrowserRouterextendsComponent{{this.history=this.state=location:this.unlisten=this.history.listen(location=>{this.setState({location}componentWillUnmount()if(this.unlisten)}{returnchildren={ps.children||null}history:this.history,location:this.state.location}實(shí)現(xiàn)exportexportfunctionRoute(props)constctx=useContext(RouterContext);const{path,component:Cmp}=props;const{location}=ctx;letmatch=path===location.pathname;returnmatch?<Cmp/>:null;}實(shí)現(xiàn)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 沉箱碼頭施工方案
- 室內(nèi)電纜敷設(shè)施工方案
- 2025年東師復(fù)試化學(xué)試題及答案
- 2025年高職數(shù)據(jù)庫試題及答案
- 5年級(jí)下冊(cè)英語第1第2單元單詞跟讀
- 5年級(jí)上冊(cè)第5單元摘抄
- 燈泡溫度與電阻的關(guān)系式
- 地下車庫 行政復(fù)議申請(qǐng)
- 機(jī)械合同的施工方案
- 2025年合肥信息技術(shù)職業(yè)學(xué)院?jiǎn)握新殬I(yè)適應(yīng)性測(cè)試題庫學(xué)生專用
- 少兒財(cái)商教育講座課件
- 醫(yī)院醫(yī)用耗材SPD服務(wù)項(xiàng)目投標(biāo)方案
- 2025年保密知識(shí)試題庫附參考答案(精練)
- 全國普通高等學(xué)校2025屆高三第二次調(diào)研數(shù)學(xué)試卷含解析
- 南昌起義模板
- “互聯(lián)網(wǎng)+”大學(xué)生創(chuàng)新創(chuàng)業(yè)大賽計(jì)劃書一等獎(jiǎng)
- 2024年10月高等教育自學(xué)考試13015計(jì)算機(jī)系統(tǒng)原理試題及答案
- GB/T 3324-2024木家具通用技術(shù)條件
- 2024秋期國家開放大學(xué)本科《古代小說戲曲專題》一平臺(tái)在線形考(形考任務(wù)4)試題及答案
- 血吸蟲病知識(shí)宣傳講座
- 詩經(jīng)的課件教學(xué)課件
評(píng)論
0/150
提交評(píng)論