# day03_react_router **Repository Path**: zangcenyang/day03_react_router ## Basic Information - **Project Name**: day03_react_router - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-11-23 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 总结 react路由 首先安装路由插件 ``` npm i react-router npm i react-router-dom ``` 创建文件Router.js ```js import React, { Component } from 'react' import { HashRouter, Switch, Route } from 'react-router-dom' class Router extends Component { render() { return (
) } } export default Router ``` #### 路由跳转有两种 **一、path** ``` //路由创建 //跳转时 this.props.history.push({ pathname:'/路由', state:{ 属性名:"传入内容" } }) //接收路由时 this.props.location.state.属性名.传入的值 ``` 缺点:传入的值不能保存,刷新页面后就没有了 解决方法:把传入的值保存在本地存储中(用临时存储sessionStorage) **二、Link** ``` //跳转 跳转页面 //动态路由 //接收 this.props.match.params.属性名 ``` ##### react 路由嵌套 只能写在需要跳转的组件中,不能写在路由文件中。 跳转方法只能用 Link 动态路由跳转。 ```js return(
页面一 页面二
//页面一开始就渲染的要写在最下面
) ```