• react路由操作


    路由

    • 需要安装react路由插件
    npm i --save react-router-dom	#功能更全
    

    hash模式

    import { HashRouter, Route, Link  } from "react-router-dom";
    
      render() {
        return (
            <HashRouter>
              <div>           
                  <header className="title">
                    <Link to="/">首页</Link>
                    <Link to="/news">新闻</Link>
                    <Link to="/product">商品</Link>
                  </header>
                   <br />
                   <hr />
                   <br />
                  <Route exact path="/" component={Home} />
                  <Route path="/news" component={News} />    
                  <Route path="/product" component={Product} />                 
              </div>
          </HashRouter>
        );
      }
    

    history模式

    import { BrowserRouter as Router, Route, Link } from "react-router-dom"
      render() {
        return (
            <Router>
              <div>           
                  <header className="title">
                    <Link to="/">首页</Link>
                    <Link to="/news">新闻</Link>
                    <Link to="/product">商品</Link>
                  </header>
                   <br />
                   <hr />
                   <br />
                  <Route exact path="/" component={Home} />
                  <Route path="/news" component={News} />    
                  <Route path="/product" component={Product} />                 
              </div>
          </Router>
        );
      }
    

    路由跳转和增加参数

    import React, { Component } from 'react';
    
    class News extends Component {
    	  constructor(props) {
    	    super(props);
    	    this.data = {
    	      value: null
    	    };
    	  }
    	componentWillMount(){
    	   console.log('测试',this.data)
    	}
    	render(){
    	  const btnNav = ()=> { 
                  this.props.history.push({
                  	pathname:'/',
                  	search:'name'
                  });
    	  }
    	  return (
    		<div className="News">
    			当前是News页面
    			<button onClick={btnNav}>跳转到Home页面</button>
    		</div>
    	  );	
    	}
    }
    export default News;
    
    # 明传
    
    this.props.history.push({
    	pathname:'/',
    	search: 'name=1'
    });
    
    
    # 暗传
    
    this.props.history.push({
    	pathname:'/',
    	state: { test: 'dashboard' }
    });
    
    # 接收参数
    
    ## Search
    
    this.props.history.location.search
    
    ## State
    
    this.props.history.location.state
    
    
  • 相关阅读:
    POJ 2923 Relocation (状态压缩,01背包)
    HDU 2126 Buy the souvenirs (01背包,输出方案数)
    hdu 2639 Bone Collector II (01背包,求第k优解)
    UVA 562 Dividing coins (01背包)
    POJ 3437 Tree Grafting
    Light OJ 1095 Arrange the Numbers(容斥)
    BZOJ 1560 火星藏宝图(DP)
    POJ 3675 Telescope
    POJ 2986 A Triangle and a Circle
    BZOJ 1040 骑士
  • 原文地址:https://www.cnblogs.com/ooo51o/p/16150175.html
Copyright © 2020-2023  润新知