鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ react-router4 如何傳遞參數(shù)并接收

react-router4 如何傳遞參數(shù)并接收

問題描述

剛開始學(xué)習(xí)react,在react-router4傳遞參數(shù)這里卡住了,請(qǐng)各位大神幫幫忙。

問題出現(xiàn)的環(huán)境背景及自己嘗試過哪些方法

相關(guān)代碼

// 請(qǐng)把代碼文本粘貼到下方(請(qǐng)勿用圖片代替代碼)

import React from 'react';
export default class ComponentList extends React.Component{

constructor (props){
    super(props)
    console.log(this.props.match.params.id);
}
render(){
    return(
        <div>
            <h2>這里是列表頁面</h2>
            <h2>id是: {this.props.match.params.id}</h2>
        </div>
    )
}

}

你期待的結(jié)果是什么?實(shí)際看到的錯(cuò)誤信息又是什么?

就是想獲取到url上的參數(shù)id,/index.html#/user/123123 就是這個(gè)123123.
console.log(this.props.match.params.id);這句話能把123123打出來。
但是 <h2>id是: {this.props.match.params.id}</h2>這里就報(bào)錯(cuò)。
我應(yīng)該怎么寫才能在return里面獲取到這個(gè)id參數(shù)呢?拜托各位求教

回答
編輯回答
伐木累
<Route path='/user/:id' component={ComponentList} />
2017年3月11日 08:57
編輯回答
忘了我
<Switch>
            <Route exact path='/' component={ Home } />
            <Route path='/about/:number' component={ About } />
</Switch>

結(jié)合

<Link key={item} to={`/about/${item}`}>about{item}</Link>
2017年1月7日 16:11