前面已经已经讲过一次路由   稍微有些复杂   考虑到有些同学刚接触到   我准备了一个简单的demo   就当自己也顺便复习一下

data.js

 const data = [
{
name: 'Tacos',
description: 'A taco (/ˈtækoʊ/ or /ˈtɑːkoʊ/) is a traditional Mexican dish composed of a corn or wheat tortilla folded or rolled around a filling. A taco can be made with a variety of fillings, including beef, pork, chicken, seafood, vegetables and cheese, allowing for great versatility and variety. A taco is generally eaten without utensils and is often accompanied by garnishes such as salsa, avocado or guacamole, cilantro (coriander), tomatoes, minced meat, onions and lettuce.',
items: [
{ name: 'Carne Asada', price: 7 },
{ name: 'Pollo', price: 6 },
{ name: 'Carnitas', price: 6 }
]
},
{
name: 'Burgers',
description: 'A hamburger (also called a beef burger, hamburger sandwich, burger or hamburg) is a sandwich consisting of one or more cooked patties of ground meat, usually beef, placed inside a sliced bun. Hamburgers are often served with lettuce, bacon, tomato, onion, pickles, cheese and condiments such as mustard, mayonnaise, ketchup, relish, and green chile.',
items: [
{ name: 'Buffalo Bleu', price: 8 },
{ name: 'Bacon', price: 8 },
{ name: 'Mushroom and Swiss', price: 6 }
]
},
{
name: 'Drinks',
description: 'Drinks, or beverages, are liquids intended for human consumption. In addition to basic needs, beverages form part of the culture of human society. Although all beverages, including juice, soft drinks, and carbonated drinks, have some form of water in them, water itself is often not classified as a beverage, and the word beverage has been recurrently defined as not referring to water.',
items: [
{ name: 'Lemonade', price: 3 },
{ name: 'Root Beer', price: 4 },
{ name: 'Iron Port', price: 5 }
]
}
] const dataMap = data.reduce((map,category) => //重点看这里 在干嘛 对es6很有帮助哟
{
category.itemMap =
category.items.reduce((itemMap,item)=>{
itemMap[item.name] = item;
return itemMap
},{})
map[category.name] = category;
return map
},
{}
) exports.getAll = function () {
return data
} exports.lookupCategory = function (name) {
return dataMap[name]
} exports.lookupItem = function (category, item) {
return dataMap[category].itemsMap[item]
}

app.js

 import React from 'react'
import { render } from 'react-dom'
import { browserHistory, Router, Route, Link } from 'react-router' import withExampleBasename from '../withExampleBasename'
import data from './data' import './app.css' const category = ({children,params})=>{
const category = data.lookupCategory(params.category) return (
<div>
<h1>{category.name}</h1>
{children || (
<p>{category.description}</p>
)}
</div>
)
} const CategorySidebar = ({ params }) => {
const category = data.lookupCategory(params.category) return (
<div>
<Link to="/">◀︎ Back</Link> // "/" 根目录 到app组件 app包含什么 往下看
<h2>{category.name} Items</h2>
<ul>
{category.items.map((item, index) => (
<li key={index}>
<Link to={`/category/${category.name}/${item.name}`}>{item.name}</Link> //见下面的route
</li>
))}
</ul>
</div>
)
} const Item = ({ params: { category, item } }) => {
const menuItem = data.lookupItem(category, item) return (
<div>
<h1>{menuItem.name}</h1>
<p>${menuItem.price}</p>
</div>
)
} const Index = () => (
<div>
<h1>Sidebar</h1>
<p>
Routes can have multiple components, so that all portions of your UI
can participate in the routing.
</p>
</div>
) const IndexSidebar = () => (
<div>
<h2>Categories</h2>
<ul>
{data.getAll().map((category, index) => (
<li key={index}>
<Link to={`/category/${category.name}`}>{category.name}</Link>
</li>
))}
</ul>
</div>
) const App = ({ content, sidebar }) => (
<div>
<div className="Sidebar">
{sidebar || <IndexSidebar />}
</div>
<div className="Content">
{content || <Index />}
</div>
</div>
) render((
<Router history={withExampleBasename(browserHistory, __dirname)}>
<Route path="/" component={App}>
<Route path="category/:category" components={{ content: Category, sidebar: CategorySidebar }}> // 一级
<Route path=":item" component={Item} /> //二级
</Route>
</Route>
</Router>
), document.getElementById('example'))

看懂了吧  很简单吧

我们再来看一个例子

 import React from 'react'
import { render } from 'react-dom'
import { browserHistory, Router, Route, Link } from 'react-router' import withExampleBasename from '../withExampleBasename' const User = ({ params: { userID }, location: { query } }) => {
let age = query && query.showAge ? '33' : '' return (
<div className="User">
<h1>User id: {userID}</h1>
{age}
</div>
)
} const App = ({ children }) => (
<div>
<ul>
<li><Link to="/user/bob" activeClassName="active">Bob</Link></li>
<li><Link to={{ pathname: '/user/bob', query: { showAge: true } }} activeClassName="active">Bob With Query Params</Link></li>
<li><Link to="/user/sally" activeClassName="active">Sally</Link></li>
</ul>
{children}
</div>
) render((
<Router history={withExampleBasename(browserHistory, __dirname)}>
<Route path="/" component={App}>
<Route path="user/:userID" component={User} />
</Route>
</Router>
), document.getElementById('example'))

简单吧 找到自信了吗 。。。

最后看一个复杂一点的   当做巩固吧

一个一个来

 import React from 'react'
import { render } from 'react-dom'
import { browserHistory, Router, Route, IndexRoute, Link } from 'react-router' import withExampleBasename from '../withExampleBasename' const PICTURES = [
{ id: 0, src: 'http://placekitten.com/601/601' },
{ id: 1, src: 'http://placekitten.com/610/610' },
{ id: 2, src: 'http://placekitten.com/620/620' }
] const Modal = React.createClass({
styles: {
position: 'fixed',
top: '20%',
right: '20%',
bottom: '20%',
left: '20%',
padding: 20,
boxShadow: '0px 0px 150px 130px rgba(0, 0, 0, 0.5)',
overflow: 'auto',
background: '#fff'
}, render() {
return (
<div style={this.styles}>
<p><Link to={this.props.returnTo}>Back</Link></p> //这里是要返回当前属性的地址
{this.props.children}
</div>
)
}
})
 const App = React.createClass({

   componentWillReceiveProps(nextProps) {  //组件改变时  就是路由切换时
// if we changed routes...
if ((
nextProps.location.key !== this.props.location.key && //经过上面的讲解这里的location.key和state 懂吧
nextProps.location.state &&
nextProps.location.state.modal
)) {
// save the old children (just like animation)
this.previousChildren = this.props.children
}
}, render() {
let { location } = this.props let isModal = (
location.state &&
location.state.modal &&
this.previousChildren
) return (
<div>
<h1>Pinterest Style Routes</h1> <div>
{isModal ?
this.previousChildren :
this.props.children
} {isModal && (
<Modal isOpen={true} returnTo={location.state.returnTo}> // 这里是往回跳转
{this.props.children} //大家要问this,props.children 是指的什么
</Modal>
)}
</div>
</div>
)
}
})

index-route  当app没有定义时  被包含进去

 const Index = React.createClass({
render() {
return (
<div>
<p>
The url `/pictures/:id` can be rendered anywhere in the app as a modal.
Simply put `modal: true` in the location descriptor of the `to` prop.
</p> <p>
Click on an item and see its rendered as a modal, then copy/paste the
url into a different browser window (with a different session, like
Chrome -> Firefox), and see that the image does not render inside the
overlay. One URL, two session dependent screens :D
</p> <div>
{PICTURES.map(picture => (
<Link
key={picture.id}
to={{
pathname: `/pictures/${picture.id}`,
state: { modal: true, returnTo: this.props.location.pathname }
}}
>
<img style={{ margin: 10 }} src={picture.src} height="100" />
</Link>
))}
</div> <p><Link to="/some/123/deep/456/route">Go to some deep route</Link></p> </div>
)
}
})

大家如果还是不很懂   可以看一下index-route  举个例子

 import React from 'react'
import { render } from 'react-dom'
import { Router, Route, hashHistory, IndexRoute } from 'react-router'
import App from './modules/App'
import About from './modules/About'
import Repos from './modules/Repos'
import Repo from './modules/Repo'
import Home from './modules/Home' render((
<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRoute component={Home}/>
<Route path="/repos" component={Repos}>
<Route path="/repos/:userName/:repoName" component={Repo}/>
</Route>
<Route path="/about" component={About}/>
</Route>
</Router>
), document.getElementById('app'))

看下app.js

 import React from 'react'
import NavLink from './NavLink' export default React.createClass({
render() {
return (
<div>
<h1>React Router Tutorial</h1>
<ul role="nav">
<li><NavLink to="/about">About</NavLink></li>
<li><NavLink to="/repos">Repos</NavLink></li>
</ul>
{this.props.children} //如果一开始访问 根目录/ 其他的字组件还没包含进去 那么。。。 看下面
</div>
)
}
})

home.js

 import React from 'react'

 export default React.createClass({
render() {
return <div>Home</div> //知道了吧 刚才进去 被包含进去的就是home组件 当进行路由跳转的时候
}
})

navLink.js

 // modules/NavLink.js
import React from 'react'
import { Link } from 'react-router' export default React.createClass({
render() {
return <Link {...this.props} activeClassName="active"/>
}
})

repo.js

 import React from 'react'

 export default React.createClass({
render() {
return (
<div>
<h2>{this.props.params.repoName}</h2>
</div>
)
}
})

repos.js

 import React from 'react'
import NavLink from './NavLink' export default React.createClass({
render() {
return (
<div>
<h2>Repos</h2>
<ul>
<li><NavLink to="/repos/reactjs/react-router">React Router</NavLink></li>
<li><NavLink to="/repos/facebook/react">React</NavLink></li>
</ul>
{this.props.children} //repos肯定有子路由
</div>
)
}
})

无关紧要的about.js

 import React from 'react'

 export default React.createClass({
render() {
return <div>About</div>
}
})

最重要的index.js

 import React from 'react'
import { render } from 'react-dom'
import { Router, Route, hashHistory, IndexRoute } from 'react-router'
import App from './modules/App'
import About from './modules/About'
import Repos from './modules/Repos'
import Repo from './modules/Repo'
import Home from './modules/Home' render((
<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRoute component={Home}/>
<Route path="/repos" component={Repos}>
<Route path="/repos/:userName/:repoName" component={Repo}/>
</Route>
<Route path="/about" component={About}/>
</Route>
</Router>
), document.getElementById('app'))

我们来整理一下

1.进入根目录/  渲染app  下面有home  和  repos(下面又有repo)

2.根据指定路径到不同组件 上面代码中,用户访问/repos时,会先加载App组件,然后在它的内部再加载Repos组件。

今天就这么多  这些基础的东西还是很重要的   话说我最近还真是忙呀

一个屁点大的公司,就我一个前端,还tm什么都要会,静态页面lz一个人些,还要做交互的特效,坑爹的事各种响应式,还非要什么flex布局。。。这还没完呀

而且搞完了lz还要去用redux开发后台页面,现在小公司没一个不坑的,借口都是培养成全站式程序员,但又没实际的培训的技术指导,总监和pm什么的都在家

遥控指挥,你觉得这不是笑话么。。。笑话归笑话,又要开始烧脑了

最新文章

  1. 关于UIScollView 中的contentOffset 的理解
  2. Super Jumping! Jumping! Jumping!
  3. [转]Servlet 3.0 新特性详解
  4. iOS状态栏字体设置为白色
  5. MFC 对话框中动态创建N级菜单以及响应事件
  6. unity 编辑器和插件生产(四.2)
  7. arcengine 开发经典帖
  8. 如何通过jQuery获取一个没有定高度的元素---------的自适应高度(offsetHeight的正确使用方法)
  9. istio添加Fluentd
  10. DSAPI 键盘鼠标钩子
  11. Linux-centos7下python3 环境设置
  12. SQL逻辑查询语句执行顺序
  13. HDU 4027 Can you answer these queries【线段树】
  14. PHP 数组转XML 格式
  15. SIM800C 连接服务器
  16. go语言从零学起(四) -- 基于martini和gorilla实现的websocket聊天实例
  17. 限制SSH用户访问Linux中指定的目录
  18. SolrServer SolrRequest
  19. MongoDB应用场景
  20. JavaScript的基础篇

热门文章

  1. 阿里云Linux安装软件镜像源
  2. 用c#开发微信 系列汇总
  3. Java提高篇(三三)-----Map总结
  4. 关于Windows高DPI的一些简单总结
  5. [蓝牙] 4、Heart Rate Service module
  6. [自己动手玩黑科技] 1、小黑科技——如何将普通的家电改造成可以与手机App联动的“智能硬件”
  7. BIT祝威博客汇总(Blog Index)
  8. Homebrew简介及安装
  9. 实战使用Axure设计App,使用WebStorm开发(5) – 实现页面功能
  10. 将图片的二进制字节 在HTML页面中显示