版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
React全家桶课堂basename:stringbasename:stringto:stringto: ce:boolto:stringto:Routerendermethodspath:stringlocation:location:objectchildren:node深入理解RouterReact中文Router-搭建cra使用HTML5提供的historypushState 和URL的同步basename:
录,则需要将其设置为子目录。basename的格式是前面有<BrowserRouter<BrowserRouter<Linkto="/user"上例中 最终将被呈现为<a<ahref="/kkb/user"使用URL
部分(window.location.hash)UIURLbasename:<HashRouter<HashRouter<Linkto="/user"上例中 最终将被呈现为<a<ahref="#/kkb/user"注意:hashhistory不支 location.state。在以前的版本中,我们曾尝试shim。BrowserRouter与HashRouter对比BrowserRouter使用HTML5historyAPI(pushState,receState和popstate),让页面的UI于URL同HashRouter不支持location.y和location.state由跳转需要通过传递参数。browserHistoryHashhistory不需要服务器任何配置就可以运行,如果你境中用到它,因为每一个browserHistory把URL的历史记录保存在内存中的<Router>(不、不importimport{MemoryRouter}from'react-<Appto: 地址,通过pathname、search和<Link<Linkto='/courses?sort=name'to:一个对象形式 地址,可以具有以下任何属性-要到的路-URL中的hash,例如#the--到location中的额外状态数<Link<Linkpathname:'/courses',search:'?sort=name',hash:'#the-hash',state:{redirect:}}}rece:
时,点 后将替换历史堆栈中的当前目,而不是添加新条目。默认为false<Link<Linkto="/courses"ce你还可以传递一些其它属性,例如title、id或<Link<Linkto="/"className="nav"title="ato:URLpath-to-regex有效的URL路径。所有要使用的URL参数必须由 <Redirect<Redirectto="/somewhere/else"to:能够理解的任何有效的URL路径
可以是path-to-<Redirect<Redirectto={{pathname:'/login',search:'?utm=your+face',state:{referrer:}}}
进行。
进行。
可能是ReactRouter助你理解和学习如何更好的使用ReactRouter
属性与某个location匹配时呈现一些UIRouterender使 渲染一些内容有以下三种方式render:funcchildren:funcpath:
path-to-regexURL<Route<Routepath="/users/:id"component={User}没有定 总是会被匹配location:<Route>
与当前location(通常是当前的浏览器URL)进行匹配。但是,带
的进行匹配时,会发现这个api非常有用。如过渡动画
的location相匹配(或者是当前的location),
所有Router组件的通用低阶接口。通常情况下,应用程序只会使用其中一个高阶Router:用于渲染与路径匹配的第一个 或<Redirect>这与仅仅使用一系 有何不同
<Route<Routepath="/about"component={About}<Routepath="/:user"component={User}<Routecomponent={NoMatch}URL是/about,那么<About>、<User>设计,允许我们以很多方式 组合成我们的应用序,例如侧边栏和面包屑、引 等
在URL
时不想匹配/:user(404页面),这该怎么实现呢?以下就是如何使用importimport{Switch,Route}from'react-<Routeexactpath="/"component={Home}<Routepath="/about"component={About}<Routepath="/:user"component={User}<Routecomponent={NoMatch}现在,当我们 路径时,<Switch>将开始寻找配的<Route><Routepath="/about会被正确匹配,这 会停止查找匹配项并立即现<About>。同样,如果我们在
{/*这里只会渲染一个子元素<Route<Route<Route<Route{/*这里总是会渲染两个子元素,也有可能是空渲染,这使得转换更加麻烦*/}location:用于匹配子元素而不是当前historylocation(通常是当前的浏览器URL)的location对象。<Switchlocation={{pathname:<Routeexactpath="/"{HomePage}<Routepath="/user"{UserPage}{/*<PrivateRoutepath="/user"component={UserPage}/>*/}<Routepath="/login"{LoginPage}<Routepath="/children"=><div>children</div>}<Routepath="/render"render={()<div>render</div>}<Routepath="/search/:id" ponent}
的子元素都应该 <Redirect>
当在<Switch>中包含<Redirect>时,你可以使用任<Route>拥有的路径匹配属性path、exactstrict。from只是path的别名如果给<Switch>提供一的子元素上的location属性
属性,它将覆盖匹<Routeexactpath="/"component={Home}<Routepath="/users"component={Users}<Redirectfrom="/accounts"to="/users"<Routecomponent={NoMatch}实测试代码importimportReactfrom//import{BrowserRouterasRouter,Route,Link,Switch}from"react-router-dom";import{BrowserRouterasRouter}from"./k-importRoutefrom"./k-react-router-importLinkfrom"./k-react-router-importimportSwitchfrom"./k-react-router-importHomePagefrom"./pages/HomePage";importUserPagefrom"./pages/UserPage";importLoginPagefrom"./pages/LoginPage";importPrivateRoutefromfunctionApp(){return(<div<Linkto="/">首页<Link<Linkto="/login">登录<Link<Link<Linkto="/search/123">搜索-动态路{/*<Switchlocation={{pathname:<Routeexactpath="/"{HomePage}{{/*<Routepath="/user"{UserPage}/><PrivateRoutepath="/user"{UserPage}<Routepath="/login"{LoginPage}<Routepath="/children"=><div>children</div>}<Routepath="/render"render={()<div>render</div>}<Routepath="/search/:id" ponent}}exportdefaultponent(props)const{id}=return(ponent-props",ponent-<Linkto={"/search/"+id+"/detail"}>情-嵌套路由<Route<Routepath={"/search/:id/detail"} ponent}/>}ponent(props)const{id}=return<div>}ponent-props",实现实现importimportReact,{Component}fromimport{createBrowserHistory}from"history";importmatchPathfrom"./matchPath";constRouterContext=exportclassBrowserRouterextendsComponentconstructor(props)this.state={};this.history=createBrowserHistory();this.state={location:this.unlisten=this.history.listen(location=>{location:location}componentWillUnmount(){if(this.unlisten){}}render(){returnvalue={{history:location:}}}实现exportexportclassRouteextendsComponent{render(){return{context=>//优先从props中取constlocation=||//优先从props中取值计constmatch :?:context.match;constprops={let{path,children,component,render}=ps;matchcomponentrender或者不match,渲染children或者nullreturn(<RouterContext.Provider
??typeofchildren?::?React.createElement(component,:?::typeofchildren
?:}}}实现Link.js:跳 ,处理点exportexportclassLinkextendsComponent{staticcontextType=RouterContext;handleClick=event=>{render()const{to}=ps;return(<ahref={to}}}实现exportclassexportclassSwitchextendsComponentrender(){return{context=>constlocation=context.location;letelement,match;child=>{if(match==null&&React.isValidElement(child)){element=constpath=ps.path;match=path?{...ps,:}returnmatchReact.cloneElement(element,{location}):}}实现importReact,{Component}fromimport{RouterContext}fromexportdefaultclassRedirectextends{render(){return{context=>const{history}=context;const{to}=ps;//history.push(location)return<LifeCycleonMount={()=> ce(to)}}}classLifeCycleextendsComponent{componentDidMount(){if(ps.onMount){
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工地临边作业安全防护
- 2025年区块链钱包开发与应用
- 徐汇高校食堂外包合同
- 项目采购服务外包合同
- 2025年氢燃料电池测试设备故障代码说明
- 甘肃省定西市临洮县2025-2026学年七年级上学期语文期末试卷(含答案)
- 伊通执业医师乡村全科测试卷
- 护理礼仪:团队协作的重要性
- 氧气疗法的种类选择与安全用氧护理
- 2026年桩基检测与评估合同三篇
- key-hole经皮内镜颈椎间盘摘除术治疗神经根型颈椎病后路2
- 金版教程物理2024导学案必修第册人教版新必修第三册含答案
- 2023-2024学年广东省佛山市顺德区八年级(下)期末数学试卷(含答案)
- JBT 14437-2023 二氧化碳致裂管 (正式版)
- 精装房行业分析研究报告
- 了解红旗渠学习红旗渠精神课件
- 2022年北京市大兴区瀛海镇社区工作者招聘考试真题及答案
- 山地光伏施工组织设计
- 儿科主治医师资格考试题库(含答案)
- 藏乐阁点歌机使用说明书
- YS/T 583-2016热锻水暖管件用黄铜棒
评论
0/150
提交评论