react路由
发布时间:2020-12-15 20:24:52 所属栏目:百科 来源:网络整理
导读:import React from ‘ react ‘ import { Router,Route,Link } from ‘ react-router ‘ const App = React.createClass({ render() { return ( div h1App/h1 ul liLink to= " /about " About/Link/li liLink to= " /inbox " Inbox/Link/li /ul { this .prop
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。 现在,App 的 render 中的 this.props.children 将会是 <Dashboard>这个元素。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |