zoukankan      html  css  js  c++  java
  • React---路由组件传参

    一、向路由组件传递参数

                    1.params参数

                                路由链接(携带参数):<Link to='/demo/test/tom/18'}>详情</Link>
                                注册路由(声明接收):<Route path="/demo/test/:name/:age" component={Test}/>
                                接收参数:this.props.match.params

                    2.search参数

                                路由链接(携带参数):<Link to='/demo/test?name=tom&age=18'}>详情</Link>
                                注册路由(无需声明,正常注册即可):<Route path="/demo/test" component={Test}/>
                                接收参数:this.props.location.search
                                备注:获取到的search是urlencoded编码字符串,需要借助querystring解析
          

                    3.state参数

                                路由链接(携带参数):<Link to={{pathname:'/demo/test',state:{name:'tom',age:18}}}>详情</Link>
                                注册路由(无需声明,正常注册即可):<Route path="/demo/test" component={Test}/>
                                接收参数:this.props.location.state
                                备注:刷新也可以保留住参数

    二、编程式路由导航

                        借助this.props.history对象上的API对操作路由跳转、前进、后退
                                -this.props.history.push()
                                -this.props.history.replace()
                                -this.props.history.goBack()
                                -this.props.history.goForward()
                                -this.props.history.go()

    三、BrowserRouter与HashRouter的区别

                1.底层原理不一样:
                            BrowserRouter使用的是H5的history API,不兼容IE9及以下版本。
                            HashRouter使用的是URL的哈希值。
                2.path表现形式不一样
                            BrowserRouter的路径中没有#,例如:localhost:3000/demo/test
                            HashRouter的路径包含#,例如:localhost:3000/#/demo/test
                3.刷新后对路由state参数的影响
                            (1).BrowserRouter没有任何影响,因为state保存在history对象中。
                            (2).HashRouter刷新后会导致路由state参数的丢失!!!
                4.备注:HashRouter可以用于解决一些路径错误相关的问题。

    四、代码

    1. 传递参数

     1 import React, { Component } from 'react'
     2 import {Link,Route} from 'react-router-dom'
     3 import Detail from './Detail'
     4 
     5 export default class Message extends Component {
     6     state = {
     7         messageArr:[
     8             {id:'01',title:'消息1'},
     9             {id:'02',title:'消息2'},
    10             {id:'03',title:'消息3'},
    11         ]
    12     }
    13     render() {
    14         const {messageArr} = this.state
    15         return (
    16             <div>
    17                 <ul>
    18                     {
    19                         messageArr.map((msgObj)=>{
    20                             return (
    21                                 <li key={msgObj.id}>
    22 
    23                                     {/* 向路由组件传递params参数 */}
    24                                     {/* <Link to={`/home/message/detail/${msgObj.id}/${msgObj.title}`}>{msgObj.title}</Link> */}
    25 
    26                                     {/* 向路由组件传递search参数 */}
    27                                     {/* <Link to={`/home/message/detail/?id=${msgObj.id}&title=${msgObj.title}`}>{msgObj.title}</Link> */}
    28 
    29                                     {/* 向路由组件传递state参数 */}
    30                                     <Link to={{pathname:'/home/message/detail',state:{id:msgObj.id,title:msgObj.title}}}>{msgObj.title}</Link>
    31 
    32                                 </li>
    33                             )
    34                         })
    35                     }
    36                 </ul>
    37                 <hr/>
    38                 {/* 声明接收params参数 */}
    39                 {/* <Route path="/home/message/detail/:id/:title" component={Detail}/> */}
    40 
    41                 {/* search参数无需声明接收,正常注册路由即可 */}
    42                 {/* <Route path="/home/message/detail" component={Detail}/> */}
    43 
    44                 {/* state参数无需声明接收,正常注册路由即可 */}
    45                 <Route path="/home/message/detail" component={Detail}/>
    46 
    47             </div>
    48         )
    49     }
    50 }

    2. 接收参数

     1 import React, { Component } from 'react'
     2 // import qs from 'querystring'
     3 
     4 const DetailData = [
     5     {id:'01',content:'你好,中国'},
     6     {id:'02',content:'你好,尚硅谷'},
     7     {id:'03',content:'你好,未来的自己'}
     8 ]
     9 export default class Detail extends Component {
    10     render() {
    11         console.log(this.props);
    12 
    13         // 接收params参数
    14         // const {id,title} = this.props.match.params 
    15 
    16         // 接收search参数
    17         // const {search} = this.props.location
    18         // const {id,title} = qs.parse(search.slice(1))
    19 
    20         // 接收state参数
    21         const {id,title} = this.props.location.state || {}
    22 
    23         const findResult = DetailData.find((detailObj)=>{
    24             return detailObj.id === id
    25         }) || {}
    26         return (
    27             <ul>
    28                 <li>ID:{id}</li>
    29                 <li>TITLE:{title}</li>
    30                 <li>CONTENT:{findResult.content}</li>
    31             </ul>
    32         )
    33     }
    34 }
     
  • 相关阅读:
    leetcode 10 正则表达式匹配(c++)
    基于.NetCore3.1系列 —— 日志记录之初识Serilog
    AspNetCore WebApi:Serilog(日志)
    .NET Core下的日志(3):如何将日志消息输出到控制台上
    Asp.Net Core用NLog记录日志操作方法
    .NET Core3.0 日志 logging-最好用的日志集合介绍
    .net core 3.1 使用nlog记录日志 NLog.Web.AspNetCore
    NetCore3.1 日志组件 Nlog的使用
    配置 ASP.NET Core 请求(Request)处理管道
    vue进入页面每次都调用methods里的方法
  • 原文地址:https://www.cnblogs.com/le220/p/14702026.html
Copyright © 2011-2022 走看看