1.Link跳转—get传值

1.1传值

  1. //1.传值
  2. import React, { Component } from 'react';
  3. import {Link} from 'react-router-dom';
  4. import {Button} from 'antd'
  5. class Home extends Component {
  6. constructor(props){
  7. super(props);
  8. this.state={
  9. id:1001
  10. }
  11. }
  12. render() {
  13. return (
  14. <div>
  15. 主页
  16. <Link to={`/detail?id=${this.state.id}`}> //传值
  17. <Button>detail</Button>
  18. </Link>
  19. </div>
  20. );
  21. }
  22. }
  23. export default Home;

1.2接收

  1. //2.接收
  2. import React,{Component} from 'react'
  3. //导入query-string模块进行解析
  4. import queryString from 'query-string';
  5. class Detail extends Component {
  6. render(){
  7. return (<div>detail</div>)
  8. }
  9. componentDidMount(){
  10. var url = this.props.location.search;
  11. var obj = queryString.parse(url)
  12. console.log(obj)
  13. }
  14. }
  15. export default Detail;

2.事件跳转 this.props.history.push()

  1. class Home extends Component {
  2. constructor(props){
  3. super(props);
  4. this.state={
  5. id:1001
  6. }
  7. }
  8. render() {
  9. return (
  10. <div>
  11. 主页
  12. <Button onClick={this.handleToggle}>detail</Button>
  13. </div>
  14. );
  15. }
  16. handleToggle=()=>{
  17. this.props.history.push(`/detail?id=${this.state.id}`)
  18. }
  19. }

3.query-string解析get传值

因为传过来的是字段,所以要安装query-string将字段转成对象 {id=123456}
  1. //1.安装
  2. yarn add query-string
  1. //2.在detail页面接收 this.props.location.search
  2. import React, { Component } from 'react';
  3. import queryString from 'query-string'
  4. class Detail extends Component {
  5. render() {
  6. return (
  7. <div>
  8. 详情页
  9. </div>
  10. );
  11. }
  12. /* cdm */
  13. componentDidMount() {
  14. var url=this.props.location.search;
  15. console.log(queryString.parse(url))
  16. }
  17. }

例题 get传id

首页传id—this.props.history.push

  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)}>
  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;

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

  1. //详情页接收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. }