码迷,mamicode.com
首页 > 其他好文 > 详细

30_react_router基本使用

时间:2019-04-18 21:37:12      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:redirect   index   row   nbsp   extend   port   dir   src   group   

项目结构:

  技术图片

技术图片
import React from ‘react‘
import {render} from ‘react-dom‘
import {BrowserRouter} from ‘react-router-dom‘

import App from ‘./components/app‘

render(
    (
        <BrowserRouter>
            <App/>
        </BrowserRouter>
    ),
    document.getElementById(‘root‘)
)
index.jsx
技术图片
import React, {Component} from ‘react‘
import {NavLink, Switch, Route, Redirect} from ‘react-router-dom‘

import About from ‘../views/about‘
import Home from ‘../views/home‘

export default class App extends Component {
    render() {
        return (
            <div>
                <div className=‘row‘>
                    <div className=‘col-xs-8 col-xs-offset-2‘>
                        <div className=‘page-header‘>
                            <h2>React Router Demo</h2>
                        </div>
                    </div>
                </div>
                <div className=‘row‘>
                    <div className=‘col-xs-2 col-xs-offset-2‘>
                        <div className=‘list-group‘>
                            <NavLink className=‘list-group-item‘ to=‘/about‘>about</NavLink>
                            <NavLink className=‘list-group-item‘ to=‘/home‘>home</NavLink>
                        </div>
                    </div>
                    <div className=‘col-xs-6‘>
                        <div className=‘panel‘>
                            <div className=‘panel-body‘>
                                <Switch>
                                    <Route path=‘/about‘ component={About}/>
                                    <Route path=‘/home‘ component={Home}/>
                                    <Redirect to=‘/home‘/>
                                </Switch>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}
app.jsx
技术图片
import React, {Component} from ‘react‘

export default class About extends Component {
    render() {
        return (
            <div>
                about route component
            </div>
        )
    }
}
about.jsx
技术图片
import React, {Component} from ‘react‘

export default class Home extends Component {
    render() {
        return (
            <div>
                home route component
            </div>
        )
    }
}
home.jsx

 

30_react_router基本使用

标签:redirect   index   row   nbsp   extend   port   dir   src   group   

原文地址:https://www.cnblogs.com/zhanzhuang/p/10732477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!