我有这样的网址:

http://localhost:3000/#/firstregistration?panel=4?codice=fea023b0cb134b845d49a789a9149ab4321574fe093a5fceac1083959e26d262


我需要从此页面的网址获取名为“ codice”的参数,并在查询中使用它。我尝试使用以下代码:

render() {
  const params = new URLSearchParams(this.props.location.search);
  const codiceHash = params.get('codice');
  console.log(params.get('codice'))
  return (
   <div className={styles}>
     <div className="notification">
       <h2>Prima Registrazione eseguita con successo</h2>
     </div>
     {this.saveEsegue(email, transactionHash , blockHash, now, "FR", codiceHash)}
   </div>
  )
}


但是我从console.log得到的是null
我究竟做错了什么?

最佳答案

您的网址无效。您不能有#,然后再有两个?在里面。

您的?codice应该是&codice

这是获得鳕鱼的一种方法



const invalidHref = "http://localhost:3000/#/firstregistration?panel=4?codice=fea023b0cb134b845d49a789a9149ab4321574fe093a5fceac1083959e26d262&somethingelse"

const codice = invalidHref.split("codice=")[1].split("&")[0];

console.log(codice)





这是在有效网址上的处理方式



const params = new URLSearchParams("http://localhost:3000/#/firstregistration?panel=4&codice=fea023b0cb134b845d49a789a9149ab4321574fe093a5fceac1083959e26d262")

const codice = params.get("codice")

console.log(codice)

09-17 08:03