在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML/ react組件 如何獲取URL 參數(shù)

react組件 如何獲取URL 參數(shù)

//客戶端
index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(<div><Router><Route path='/supplies/:client_id' component={App}></Route></Router></div>, document.getElementById('root'));

App.js

class App extends Component {
  state = {users: [],
          products:[]};
  
  steps = ['紡紗','織布','后整','檢查入庫(kù)','發(fā)貨'];

  componentDidMount() {
    console.log(this.props.params.client_id);  //不對(duì)這里
    fetch('/supplies/320001')
      .then(res => res.json())
      .then(products => this.setState({ products }));
  }

訪問(wèn)
http://localhost:3000/supplies/320001
http://localhost:3000/supplies/420001

如何獲取320001 或 420001

回答
編輯回答
赱丅呿

@說(shuō)好的一血呢 this是當(dāng)前組件, http://localhost:3000/supplies/320001 320001是client_id.
是不是用的不對(duì)?

2018年6月24日 07:04
編輯回答
醉淸風(fēng)

你把this.props打印出來(lái)看看。與路由有關(guān)的參數(shù)有location history match.

所以你傳的參數(shù)在match中。并非params,這個(gè)是舊版本router的參數(shù)。

2017年10月14日 17:11
編輯回答
避風(fēng)港
this.props.params.client_id你是在哪里定義的。

打印下this,看是不是當(dāng)前組件的實(shí)例

另外打印下 this.props.match.params
2017年9月21日 19:48