美文网首页React.js
react地址栏获取查询值 query-parameters

react地址栏获取查询值 query-parameters

作者: tomorrow_chen | 来源:发表于2019-01-21 15:34 被阅读28次

    https://reacttraining.com/react-router/web/example/query-parameters

    React Router does not have any opinions about how your parse URL query strings. Some applications use simple key=value query strings, but others embed arrays and objects in the query string. So it's up to you to parse the search string yourself.
    In modern browsers that support the URL API, you can instantiate a URLSearchParams object from location.search and use that.
    In browsers that do not support the URL API (read: IE) you can use a 3rd party library such asquery-string.

    传参

    <Link to={{ pathname: "/account", search: "?name=netflix&age=18" }}>
        Netflix
    </Link>
    

    or

    this.props.history.push({ pathname: '/account', search: `?name=${netflix}&age=18` })
    

    取参

      const params = new URLSearchParams(this.props.location.search)
      console.log(params.get("name"), params.get('age'))
    
    

    相关文章

      网友评论

        本文标题:react地址栏获取查询值 query-parameters

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