美文网首页
react+dva项目中如何获取页面传递过来的参数

react+dva项目中如何获取页面传递过来的参数

作者: 不不作为 | 来源:发表于2020-01-13 11:40 被阅读0次

在元素中加入点击事件,通过路径携带参数

 onClick={()=>{history.push('/one?type='+7)}}

在目标页面:
先引入queryString 方法

const queryString = require('query-string');

生命周期componentWillMount / componentDidMount 中 通过location.search 方法 获取:

 componentWillMount/componentDidMount(){
    const { location } = this.props
    const info = location.search.replace("?", "");
    const info_ = queryString.parse(info);
    this.setState({
      type: info_.type,
    })
  }

此时 state中的type 已经为传递过来的值 即 7;

相关文章

网友评论

      本文标题:react+dva项目中如何获取页面传递过来的参数

      本文链接:https://www.haomeiwen.com/subject/dtmgactx.html