React 路由配置

转载https://blog.csdn.net/We_jia/article/details/89353729#%E4%B8%80%E3%80%81%E8%B7%AF%E7%94%B1%E9%85%8D%E7%BD%AE

React 路由配置

目录

一、路由配置

二、路由传值

2.1 静态传值

2.2 动态传值

2.3 es6模板传值

2.4 传多个值

2.5 get 传值

 三、配置子路由

四、编程式路由

4.1 同级路由跳转

4.2 子父路由跳转


一、路由配置

  • 1.安装路由
cnpm install react-router-dom –save
 
 
  • 2.配置路由

新建一个Main文件来封装路由,创建三个组件Home.jsx,News.jsx,My.jsx,在main.jsx 文件中配置这三个组件的路由

配置路由时要先导入路由,最后要在App.js中导入Main.jsx

import {BrowserRouter as Router,Route,Link} from "react-router-dom"
 
 

 
 
  1. import React,{Component} from "react";
  2. //在哪使用路由 就在哪导入路由
  3. import {BrowserRouter as Router,Route,Link} from "react-router-dom"
  4. //导入组件
  5. import Home from "./Home";
  6. import News from "./News";
  7. import My from "./My";
  8. class Main extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = { };
  12. }
  13. render() {
  14. return (
  15. <Router>
  16. {/* 路由入口 */}
  17. <Link to="/">Home </Link>
  18. <Link to="/news">News </Link>
  19. <Link to="/my">My </Link>
  20. {/* 配置路由 */}
  21. <Route path="/" component={Home}> </Route>
  22. <Route path="/news" component={News}> </Route>
  23. <Route path="/my" component={My}> </Route>
  24. </Router>
  25. );
  26. }
  27. }
  28. export default Main;

二、路由传值

2.1 静态传值


 
 
  1. import React,{Component} from "react";
  2. //在哪使用路由 就在哪导入路由
  3. import {BrowserRouter as Router,Route,Link} from "react-router-dom"
  4. //导入组件
  5. import Home from "./Home";
  6. import News from "./News";
  7. import My from "./My";
  8. class Main extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. };
  13. }
  14. render() {
  15. return (
  16. <Router>
  17. {/* 路由入口 */}
  18. <Link to="/">Home </Link>
  19. {/* 1.固定传值 */}
  20. <Link to="/news/1">News </Link>
  21. <Link to="/my">My </Link>
  22. {/* 配置路由 */}
  23. <Route path="/" component={Home}> </Route>
  24. <Route path="/news/:id" component={News}> </Route>
  25. <Route path="/my" component={My}> </Route>
  26. </Router>
  27. );
  28. }
  29. }
  30. export default Main;

2.2 动态传值


 
 
  1. import React,{Component} from "react";
  2. //在哪使用路由 就在哪导入路由
  3. import {BrowserRouter as Router,Route,Link} from "react-router-dom"
  4. //导入组件
  5. import Home from "./Home";
  6. import News from "./News";
  7. import My from "./My";
  8. class Main extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. id: 10,
  13. };
  14. }
  15. render() {
  16. return (
  17. <Router>
  18. {/* 路由入口 */}
  19. <Link to="/">Home </Link>
  20. {/* 2.动态传值 + 拼接 在子组件中接收值*/}
  21. {/* <Link to={"/news/"+this.state.id}>News </Link> */}
  22. <Link to="/my">My </Link>
  23. {/* 配置路由 */}
  24. <Route path="/" component={Home}> </Route>
  25. <Route path="/news/:id" component={News}> </Route>
  26. <Route path="/my" component={My}> </Route>
  27. </Router>
  28. );
  29. }
  30. }
  31. export default Main;

2.3 es6模板传值


 
 
  1. import React,{Component} from "react";
  2. //在哪使用路由 就在哪导入路由
  3. import {BrowserRouter as Router,Route,Link} from "react-router-dom"
  4. //导入组件
  5. import Home from "./Home";
  6. import News from "./News";
  7. import My from "./My";
  8. class Main extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. id: 10,
  13. };
  14. }
  15. render() {
  16. return (
  17. <Router>
  18. {/* 路由入口 */}
  19. <Link to="/">Home </Link>
  20. {/* 3.es6模板传值 */}
  21. {/* ``不是单引号 tab上方的键 */}
  22. <Link to={`/news/${this.state.id}`}>News </Link>
  23. <Link to="/my">My </Link>
  24. {/* 配置路由 */}
  25. <Route path="/" component={Home}> </Route>
  26. <Route path="/news/:id" component={News}> </Route>
  27. <Route path="/my" component={My}> </Route>
  28. </Router>
  29. );
  30. }
  31. }
  32. export default Main;

2.4 传多个值


 
 
  1. import React,{Component} from "react";
  2. //在哪使用路由 就在哪导入路由
  3. import {BrowserRouter as Router,Route,Link} from "react-router-dom"
  4. //导入组件
  5. import Home from "./Home";
  6. import News from "./News";
  7. import My from "./My";
  8. class Main extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. id: 10,
  13. name: "jia"
  14. };
  15. }
  16. render() {
  17. return (
  18. <Router>
  19. {/* 路由入口 */}
  20. <Link to="/">Home </Link>
  21. {/* 4.传多个值 */}
  22. <Link to={`/news/${this.state.id}/${this.state.name}`}>News </Link>
  23. <Link to="/my">My </Link>
  24. {/* 配置路由 */}
  25. <Route path="/" component={Home}> </Route>
  26. <Route path="/news/:id/:name" component={News}> </Route>
  27. <Route path="/my" component={My}> </Route>
  28. </Router>
  29. );
  30. }
  31. }
  32. export default Main;

以上几种方法在对应的组件中接收值

渲染完成之后 接收传值   componentDidMount()生命周期


 
 
  1. import React,{Component} from "react";
  2. class News extends Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = { };
  6. }
  7. //渲染完成之后 接收传值
  8. componentDidMount(){
  9. console.log( this.props.match.params)
  10. }
  11. render() {
  12. return (
  13. <div>News</div>
  14. );
  15. }
  16. }
  17. export default News;

2.5 get 传值

相当于在路径上加?id=1


 
 
  1. import React,{Component} from "react";
  2. //在哪使用路由 就在哪导入路由
  3. import {BrowserRouter as Router,Route,Link} from "react-router-dom"
  4. //导入组件
  5. import Home from "./Home";
  6. import News from "./News";
  7. import My from "./My";
  8. class Main extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = { };
  12. }
  13. render() {
  14. return (
  15. <Router>
  16. {/* 路由入口 */}
  17. <Link to="/">Home </Link>
  18. <Link to="/news">News </Link>
  19. //get传值
  20. <Link to="/my/?id=1&name=2">My </Link>
  21. {/* 配置路由 */}
  22. <Route path="/" component={Home}> </Route>
  23. <Route path="/news/:id/:name" component={News}> </Route>
  24. <Route path="/my" component={My}> </Route>
  25. </Router>
  26. );
  27. }
  28. }
  29. export default Main;

在My.jsx组件中接收值

对于这样的值可以自己写算法解析  也可以导入node的url模块进行解析

算法解析


 
 
  1. import React,{Component} from "react";
  2. class My extends Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = { };
  6. }
  7. componentDidMount(){
  8. console.log( this.props.location.search) //?id=1&name=2
  9. // 对于这样的值可以自己写方法解析 也可以导入node的url模块进行解析
  10. let url= this.props.location.search;
  11. this.changeUrl(url);
  12. }
  13. changeUrl= (url)=>{
  14. let u=url.replace( "?", "");
  15. console.log(u)
  16. let ua=u.replace( /=/g, ":").replace( "&", ",");
  17. console.log(ua)
  18. console.log(ua.split( ",")[ 0].split( ":")[ 1]);
  19. //把一个字符串分割成字符串数组:
  20. }
  21. render() {
  22. return (
  23. <div>My</div>
  24. );
  25. }
  26. }
  27. export default My;

导入url模块解析

安装url

cnpm install url --save
 
 

 
 
  1. import React,{Component} from "react";
  2. //导入url
  3. import url from "url"
  4. class My extends Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = { };
  8. }
  9. componentDidMount(){
  10. console.log( this.props.location.search) //?id=1&name=2
  11. let localurl= this.props.location.search
  12. let src=url.parse(localurl, true)
  13. console.log(src)
  14. }
  15. render() {
  16. return (
  17. <div>My</div>
  18. );
  19. }
  20. }
  21. export default My;

 三、配置子路由


 
 
  1. import React,{Component} from "react";
  2. import {BrowserRouter as Router,Route,Link} from "react-router-dom";
  3. import Child from "./Child"
  4. class Home extends Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. id: 10
  9. };
  10. }
  11. render() {
  12. return (
  13. <div>Home
  14. <Router>
  15. <Link to={"/home/child/"+this.state.id}>child </Link>
  16. <Route path="/home/child/:id" component={Child}> </Route>
  17. </Router>
  18. </div>
  19. );
  20. }
  21. }
  22. export default Home;

四、编程式路由

4.1 同级路由跳转

在News.jsx组件中写编程式路由跳转到同级路由 My.jsx


 
 
  1. import React,{Component} from "react";
  2. class News extends Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = { };
  6. }
  7. //渲染完成之后 接收传值
  8. componentDidMount(){
  9. console.log( this.props.match.params)
  10. }
  11. //编程式路由 路由条状
  12. toMy= ()=>{
  13. this.props.history.push( "/my")
  14. }
  15. render() {
  16. return (
  17. <div>
  18. News
  19. <button onClick={this.toMy}>跳转到同级路由 </button>
  20. </div>
  21. );
  22. }
  23. }
  24. export default News;

4.2 子父路由跳转

从News.jsx组件跳到父路由组件Home.jsx,以及跳动同级路由My.jsx组件

(不能跳转到爷路由)

跳转路由时可以传参


 
 
  1. import React,{Component} from "react";
  2. import {BrowserRouter as Router,Route,Link} from "react-router-dom";
  3. import Child from "./Child";
  4. class News extends Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = { };
  8. }
  9. //渲染完成之后 接收传值
  10. componentDidMount(){
  11. console.log( this.props.match.params)
  12. }
  13. //编程式路由 路由跳转
  14. toHome= ()=>{
  15. //跳转路由是可以传值
  16. this.props.history.push( "/",{ id: 1})
  17. // this.props.history.push("/")
  18. }
  19. toMy= ()=>{
  20. this.props.history.push( "/my")
  21. }
  22. render() {
  23. return (
  24. <div>
  25. News
  26. <button onClick={this.toHome}>跳转到父级路由 </button>
  27. <button onClick={this.toMy}>跳转到同级路由 </button>
  28. <Router>
  29. <Link to={"/news/child"}>child </Link>
  30. <Route exact path="/news/child" component={Child}> </Route>
  31. </Router>
  32. </div>
  33. );
  34. }
  35. }
  36. export default News;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值