近来很忙,学了一波react,特来记一笔,分享下react-router的使用方式

第一步引入内部组件

import {Route,BrowserRouter as Router,Switch,Link } from "react-router-dom"

1)配置路由文件,一个路由对应一个组件,平级路由

import SelfHeader from './components/pc/selfHeder'
import ModuleParts from './components/pc/modulePart'
import PCAddModule from './components/pc/addModules'
import detail from './components/pc/detail'
class App extends Component {
render() {
return (
<div className="App">
{/* pc */}
<SelfHeader/>
       <Link to='/' className="clearfix">addModule</Link>
       <Link to='/addModule' className="clearfix">addModule</Link>
          <Router>
<Switch >
<Route exact path="/" component={ModuleParts}></Route>
<Route path="/addModule" component={PCAddModule}></Route>
{/*
<Route path="/usercenter" component={PCUserCenter}></Route> */}
</Switch >
</Router>
</div>
);
}
}
export default App;

可以看到Link就是路由触发标签,相当于angularJS的ui-sref,vue中的route-link,在react中我们可以把Route组件当作是一个容器,对应装载入组件,类似于angularJS的ui-view(ui-router),vue中的route-view

2)路由的嵌套

经常遇到的情况是需要使用到路由嵌套,就是父子级路由

在angularJS(mvc)中我们会使用多个views,例如:

define(['app'], function (myapp) {
myapp.config(['$stateProvider', '$urlRouterProvider',
function ($stateProvider, $urlRouterProvider) {
$stateProvider.state('editor', {
url: '/editor',
//templateUrl: 'html/login.html',
//constroller: 'loginCtrl',
//resolve: {
// deps: $requireProvider.requireJS(['js/ctrls/login.js'])
//}
views: {
"main": {
templateUrl: 'html/editor.html',
controller: 'editorCtrl',
}
}
});
}
]); })

在vue2.0中这样的,不要忘记在父级组件中加上一个<router-view/>

{
path: '/v_next_tick',
name: 'v_next_tick',
component: VNextTick,
children:[
{ path: '/aaa',
name: 'aa',
component: aa,}
],
meta:{
tabNum: 0
}

在react中,下面的addModules组件承接就是上面例子中的addModules组件

class addModules extends Component {
constructor(props){
super(props)
this.state= {
a:33
}
};
render () {
return (
<div className="add-module-wrapper">
          <Link to={`${this.props.match.url}/ee/22`} className="clearfix">detailBtn</Link>
<Route exact path={`${this.props.match.url}`} component={detail}></Route>
<Route path={`${this.props.match.url}/ee/:id`} component={detail}></Route>
<Link to='/' className="clearfix">addModule</Link> {/*返回顶级默认路由对应页面*/}
</div>
)
}
}
export default addModules;

  

a)需要补充说明的是在react中子级路由是随父级组件一起配置的

react-router4.0的使用的更多相关文章

  1. [web前端] react router4.0 登录后返回之前浏览页面(回到来源页)

    本文链接:https://blog.csdn.net/zeroyulong/article/details/81911704困扰了好久的问题,最终还是在官方文档上找到了答案(看英文文档真心难受啊~~) ...

  2. Android 工程集成React Native 0.44 注意点

    当前(2017年5月22日 )React Native开发相当火爆,但是搜索下来,没有最新版本0.44集成的教程,因此尝试了一下如何集成到Android原生工程中去.本篇旨在记录出现的问题以及应对方式 ...

  3. React Native 0.50版本新功能简介

    React Native在2017年经历了众多版本的迭代,从接触的0.29版本开始,到前不久发布的0.52版本,React Native作为目前最受欢迎的移动跨平台方案.虽然,目前存在着很多的功能和性 ...

  4. 盘点 React 16.0 ~ 16.5 主要更新及其应用

    目录 0. 生命周期函数的更新 1. 全新的 Content API 2. React Strict Mode 3. Portal 4. Refs 5. Fragment 6. 其他 7. 总结 生命 ...

  5. 新手级配置 react react-router4.0 redux fetch sass

    前言 最近公司来了几个实习生,刚好我手头没什么要紧事,然后领导让我带他们学习react, 为下一个react项目做基础. 然后随手写了几个demo,帮助他们了解正经项目如何去构建配置项目. 现在分享出 ...

  6. react native 0.55.4 rctsrwebsocket会崩溃的问题解决 直接原文覆盖

    //// Copyright 2012 Square Inc.//// Licensed under the Apache License, Version 2.0 (the "Licens ...

  7. react native 0.50与OC交互 && Swift与RN交互

    新公司也打算做rn,还是得捡起来再度学习.开撸! react native一个版本一个样子,之前写的rn与iOS交互系列在最新版本中有点出入(0.50.4版本).今天填一下坑. 首先上npm版本,re ...

  8. react native 0.56.0版本在windows下有bug不能正常运行

    react native的0.56.0版本在windows下有bug不能正常运行请init 0.55.4的版本 react-native init MyApp --version 0.55.4 注意v ...

  9. React Native 0.56.1初始化项目运行出现错误(Module `AccessibilityInfo` does not exist in the Haste module map)

    当使用react-native init myApp初始化项目时,出现以下错误 出现以上错误的原因是因为0.56.1版本初始化项目就有问题,请见 https://github.com/facebook ...

  10. router4.0

    https://blog.csdn.net/sinat_17775997/article/details/69218382 React Router 4.0 实现路由守卫   https://www. ...

随机推荐

  1. spark collect获取所有元素

    from pyspark import SparkConf, SparkContext conf = SparkConf().setMaster("local").setAppNa ...

  2. dup的使用(二)

    转自:http://blog.csdn.net/yeyuangen/article/details/6852682 一个进程在此存在期间,会有一些文件被打开,从而会返回一些文件描述符,从shell中运 ...

  3. PHP:第四章——PHP数组转换,统计,相关函数

    <pre> <?php //数组转换,统计,相关函数 header("Content-Type:text/html;charset=utf-8"); //coun ...

  4. POJ 2663 Tri Tiling 矩阵快速幂 难度:3

    Tri Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7841   Accepted: 4113 Descri ...

  5. FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

    vue项目 npm run dev 报错 WAIT Compiling...16:36:21 95% emittingFATAL ERROR: CALL_AND_RETRY_LAST Allocati ...

  6. Webbrowser checkbox

    var  i:integer;  t:OleVariant;begin  t := EmbeddedWB1.Document;  for i := 0 to t.all.length - 1 do   ...

  7. 【转】个人总结-网络安全学习和CTF必不可少的一些网站

    转自:http://blog.csdn.net/ida0918/article/details/52730662 学习的地方很多,不能一一列举,一些优秀的网址和博客可能也没有提到,大家补充吧:P就简单 ...

  8. 打开ahci模式

    打开ahci模式(sata)的正确步骤:    1.安装系统前在bios中将硬盘模式设置为ide兼容模式(别说你不会哦),然后安装系统,建议安装微软原版系统或是官方oem原版系统:    2.系统安装 ...

  9. spring 定时任务参数配置详解

    注:本文摘自<Quartz Cron 触发器 Cron Expression 的格式>http://blog.csdn.net/yefengmeander/article/details/ ...

  10. Python学习(006)-深浅拷贝及集合

     深浅拷贝 import copy husband=['xiaoxin',123,[200000,100000]] wife=husband.copy() #浅拷贝 wife[0]='xiaohong ...