react路由操作


路由

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

hash模式

import { HashRouter, Route, Link  } from "react-router-dom";

  render() {
    return (
        
          
首页 新闻 商品



); }

history模式

import { BrowserRouter as Router, Route, Link } from "react-router-dom"
  render() {
    return (
        
          
首页 新闻 商品



); }

路由跳转和增加参数

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 (
		
当前是News页面
); } } 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