zoukankan      html  css  js  c++  java
  • react-router基本用法

    1.引入

    $ yarn add react-router-dom
    import {HashRouter as Router,Route,Link} from 'react-router-dom';
    

      

    2.配置

    ReactDOM.render(
    		<Router>
    			<Wrapper>
    				<Route path='/a' component={A}/>
    				<Route path='/b' component={B}/>
    			</Wrapper>
    		</Router>,
    	document.getElementById('app')
    )
    

      

    3.使用

    //容器
    class Wrapper extends React.Component {
    	constructor(props){
    		super	(props)
    	}
    
    	render() {
    		return (
    			<div>
    				<Link to='/a'>组件A</Link> <br/>
    				<Link to='/b'>组件B</Link>
    				{this.props.children}
    		 </div>
    		)
    
    	}
    }
    

    4.显示

    5.完整代码

    import React from 'react';
    import ReactDOM from 'react-dom';
    // import {BrowerRouter as Router,Switch,Route,Link} from '';
    import {HashRouter as Router,Route,Link} from 'react-router-dom';
    // import Home from 'page/home/index.jsx';
    class A extends React.Component {
    	constructor (props){
    		super	(props)
    
    	}
    	render(){
    		return <div>component A</div>
    
    	}
    }
    class B extends React.Component {
    	constructor (props){
    		super	(props)
    	}
    	render(){
    		return <div>component B</div>
    
    	}
    }
    //容器
    class Wrapper extends React.Component {
    	constructor(props){
    		super	(props)
    	}
    
    	render() {
    		return (
    			<div>
    				<Link to='/a'>组件A</Link> <br/>
    				<Link to='/b'>组件B</Link>
    				{this.props.children}
    		 </div>
    		)
    
    	}
    }
    
    ReactDOM.render(
    		<Router>
    			<Wrapper>
    				<Route path='/a' component={A}/>
    				<Route path='/b' component={B}/>
    			</Wrapper>
    		</Router>,
    	document.getElementById('app')
    )
    

      

  • 相关阅读:
    SQLite数据库如何存储和读取二进制数据
    JS常用代码
    获取内容第一张图片地址的函数
    我对App_Code的理解
    VS2008中Web项目的App_Code目录问题
    jQuery 浮动广告实现代码
    常用的倒计时脚本
    IE8 CSS hack
    精典的网吧常用批处理收集
    ASP.NET中App_Code,App_Data等文件夹的作用
  • 原文地址:https://www.cnblogs.com/ipoodle/p/9690289.html
Copyright © 2011-2022 走看看