Link传id—通过事件传参的方法传id

事件传参:https://www.yuque.com/u238997/wuotii/zt6eab

  1. import React, { Component } from 'react';
  2. import './Movie.css'
  3. class Movie extends Component {
  4. constructor(props){
  5. super(props);
  6. this.state={
  7. subjects:[]
  8. }
  9. }
  10. render() {
  11. return (
  12. <div className="container">
  13. {this.state.subjects.map(item=>{
  14. return(<div className="list" key={item.id}
  15. onClick={this.handleClick.bind(this,item.id)}> //通过事件传参将id传到详情页
  16. <img className="pic" src={item.images.small}></img>
  17. <p className="title">{item.title}</p>
  18. </div>)
  19. })}
  20. </div>
  21. );
  22. }
  23. componentDidMount() {
  24. var url="http://douban.uieee.com/v2/movie/top250"
  25. this.$http.get(url).then(res=>{
  26. var subjects=res.data.subjects;
  27. this.setState({
  28. subjects
  29. })
  30. })
  31. }
  32. handleClick(id){
  33. this.props.history.push(`/moviedetail?id=${id}`)
  34. }
  35. }
  36. export default Movie;

详情页接收id this.props.location.search

query-string解析get传值

  1. //1.安装
  2. yarn add query-string
  3. //2.detail页面导入
  4. import queryString from 'query-string'
  5. //3.detail页面接收id
  6. componentDidMount() {
  7. var id=queryString.parse(this.props.location.search) ; //取到的是对象
  8. console.log(id)
  9. var id=id.id; //取id后面的数据
  10. this.setState({
  11. id
  12. })
  13. }
  1. //4.详情页接收id并发送请求
  2. componentDidMount() {
  3. var id=queryString.parse(this.props.location.search) ; //取到的是对象
  4. console.log(id)
  5. var id=id.id; //取id后面的数据
  6. this.setState({
  7. id
  8. })
  9. this.$http.get(`https://douban-api.uieee.com/v2/movie/subject/${id}`).then(res=>{
  10. ...
  11. }
  12. }