react-router4.0的使用
近来很忙,学了一波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/>
<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的使用的更多相关文章
- [web前端] react router4.0 登录后返回之前浏览页面(回到来源页)
本文链接:https://blog.csdn.net/zeroyulong/article/details/81911704困扰了好久的问题,最终还是在官方文档上找到了答案(看英文文档真心难受啊~~) ...
- Android 工程集成React Native 0.44 注意点
当前(2017年5月22日 )React Native开发相当火爆,但是搜索下来,没有最新版本0.44集成的教程,因此尝试了一下如何集成到Android原生工程中去.本篇旨在记录出现的问题以及应对方式 ...
- React Native 0.50版本新功能简介
React Native在2017年经历了众多版本的迭代,从接触的0.29版本开始,到前不久发布的0.52版本,React Native作为目前最受欢迎的移动跨平台方案.虽然,目前存在着很多的功能和性 ...
- 盘点 React 16.0 ~ 16.5 主要更新及其应用
目录 0. 生命周期函数的更新 1. 全新的 Content API 2. React Strict Mode 3. Portal 4. Refs 5. Fragment 6. 其他 7. 总结 生命 ...
- 新手级配置 react react-router4.0 redux fetch sass
前言 最近公司来了几个实习生,刚好我手头没什么要紧事,然后领导让我带他们学习react, 为下一个react项目做基础. 然后随手写了几个demo,帮助他们了解正经项目如何去构建配置项目. 现在分享出 ...
- react native 0.55.4 rctsrwebsocket会崩溃的问题解决 直接原文覆盖
//// Copyright 2012 Square Inc.//// Licensed under the Apache License, Version 2.0 (the "Licens ...
- react native 0.50与OC交互 && Swift与RN交互
新公司也打算做rn,还是得捡起来再度学习.开撸! react native一个版本一个样子,之前写的rn与iOS交互系列在最新版本中有点出入(0.50.4版本).今天填一下坑. 首先上npm版本,re ...
- 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 ...
- 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 ...
- router4.0
https://blog.csdn.net/sinat_17775997/article/details/69218382 React Router 4.0 实现路由守卫 https://www. ...
随机推荐
- httpclient 相关使用介绍
httpclient中sessionId的获取与设置 public class HttpSessionId { public static void main(String[] args) throw ...
- SSH key的烦恼
ssh key即公钥.私钥对,使用这种方式比较安全也比较方便,可以不用输入用户名.密码. 配置步骤: 1.生成ssh key: ssh-keygen -t rsa -C "bjhuxiaol ...
- strtus2 文件上传
struts2和spring mvc上传都是用 common-fileupload来实现 1.struts2上的方式需要在对应的Action,加上如下的属性以及get/set方法 private Fi ...
- anu - reactShime
import { Component } from "./Component"; import { options } from "./util"; impor ...
- tp5中捕获异常的配置
首选在配置文件中加入配置如下 // 异常处理handle类 留空使用 \think\exception\Handle 'exception_handle' => '\\app\ ...
- webView的使用以及总结
一.webview是什么? Android WebView 做为承载网页的载体控件,他在网页显示的过程中会产生一些事件,并回调给我们的应用程序,以便我们在网页加载过程中做应用程序想处理的事情.比如说客 ...
- Ubuntu 16.04 LTS安装 cuda8.0
参考文献: http://blog.csdn.net/autocyz/article/details/52299889 http://blog.csdn.net/lixintong1992/artic ...
- Ubuntu python-opcua Test
/********************************************************************************* * Ubuntu python-o ...
- Unity3D安卓程序中提示窗与常用静态方法封装
Unity3D/安卓封装SDK常用方法 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 IO -- - ...
- Ubuntu终端及VI 快捷键
Ubuntu终端 快捷键 功能 Tab 自动补全 Ctrl+a 光标移动到开始位置 Ctrl+e 光标移动到最末尾 Ctrl+k 删除此处至末尾的所有内容 Ctrl+u 删除此处至开始的所有内容 Ct ...