react路由

摘要:
}})React.render现在,App的render中的this.props.children将会是这个元素。这个功能类似Apache的DirectoryIndex以及nginx的index指令,上述功能都是在当请求的URL匹配某个目录时,允许你制定一个类似index.html的入口文件。
import React from 'react'import { Router, Route, Link } from 'react-router'

const App =React.createClass({
  render() {
    return(
      <div>
        <h1>App</h1>
        <ul>
          <li><Link to="/about">About</Link></li>
          <li><Link to="/inbox">Inbox</Link></li>
        </ul>{this.props.children}
      </div>)
  }
})

const About =React.createClass({
  render() {
    return <h3>About</h3>}
})

const Inbox =React.createClass({
  render() {
    return(
      <div>
        <h2>Inbox</h2>{this.props.children || "Welcome to your Inbox"}
      </div>)
  }
})

const Message =React.createClass({
  render() {
    return <h3>Message {this.props.params.id}</h3>}
})

React.render((
  <Router>
    <Route path="/" component={App}>
      <Route path="about" component={About} />
      <Route path="inbox" component={Inbox}>
        <Route path="messages/:id" component={Message} />
      </Route>
    </Route>
  </Router>), document.body)
this.props.location.query.bar和this.props.match,params.id效果一样

路由的嵌套使用

想象一下当 URL 为 / 时,我们想渲染一个在 App 中的组件。不过在此时,App 的 render 中的 this.props.children 还是 undefined。
这种情况我们可以使用 IndexRoute 来设置一个默认页面。 import { IndexRoute }
from 'react-router' const Dashboard =React.createClass({ render() { return <div>Welcome to the app!</div>} }) React.render(( <Router> <Route path="/" component={App}>{/*当 url 为/时渲染 Dashboard */} <IndexRoute component={Dashboard} /> <Route path="about" component={About} /> <Route path="inbox" component={Inbox}> <Route path="messages/:id" component={Message} /> </Route> </Route> </Router>), document.body)
现在,App 的 render 中的 this.props.children 将会是 <Dashboard>这个元素。
这个功能类似 Apache 的DirectoryIndex 以及 nginx的 index指令,上述功能都是在当请求的 URL 匹配某个目录时,允许你制定一个类似index.html的入口文件。 我们的 sitemap 现在看起来如下: URL 组件
/ App ->Dashboard /about App ->About /inbox App ->Inbox /inbox/messages/:id App -> Inbox -> Message

免责声明:文章转载自《react路由》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇关于linux或者mac apache重启服务Ogre内部渲染流程分析系列下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

不难懂--------react笔记

  在jsx中不能使用class定义类名   因为class在js中是用来定义类的  定义类名的时候用className       label中的for必须写成htmlFor         ReactDOM.render:             参数1:需要渲染的dom元素或者组件         参数2:需要将渲染好的元素挂载在哪个挂载点身上...

react基础用法

React的模板是采用jsx语法   Html语法该怎么写就怎么写,遇到js代码放到大括号({})里面   注意:     1、script标签的type必须是text/babel类型     2、使用的react.js、react-dom.js、Browser.js这三个插件的位置不能乱放       react.js是react的核心       re...

Reactjs相比较原生方案是绝对的快吗?哪些情况下React有优势

作者:尤雨溪链接:http://www.zhihu.com/question/31809713/answer/53544875来源:知乎著作权归作者所有,转载请联系作者获得授权。   1. 原生 DOM 操作 vs. 通过框架封装操作。这是一个性能 vs. 可维护性的取舍。框架的意义在于为你掩盖底层的 DOM 操作,让你用更声明式的方式来描述你的目的,从...

react 执行 yarn build ,无法直接打开dist文件下的index

如果你使用create-react-app创建项目,执行命令 yarn build 后,直接以静态方式打开build文件夹内的index.html,会看到页面显示出现问题,打开console后会看到js、css、svg等文件的路径出现问题。 解释: 在打包之前,在 package.json 中 private 下(位置任意)添加"homepage": "....

React Native之React Navigation踩坑

自动重装系统之后,已经很长一段时间没有来写React Native了,今天空闲之余,决定重新配置React Native的开发环境,继续踩坑... React Native的开发环境配置狠简单,只要依照网上给出的步骤,复制粘贴,在终端下操作就行。 React Native中文网开发环境配置 当一切都已完成之后,我怀着激动的心情,打开了Xcode,尝试运行一...

jekins接通gitee的webhook做自动部署 vue、react、java、springBoot

简介 其实点一下,也是浪费生命,不是吗? 推送代码到Gitee时,由配置的 WebHook 触发 Jenkins 任务构建。多好! jekins安装插件 搜索并安装这两个插件Gitee Plugin、Jersey 2 API 第二个插件本不是必要的,是因为我目前当前Gitee Plugin插件有一个Bug,因此安装的。 安装完成后,记得重启下jekins...