如何使用我们从reactjs中的url获取的初始化值

问题描述 投票:0回答:3

我正在研究reactjs前端项目。我想从网址获取参数并将其保存在另一个变量中。 const id = {match.params.pid};

它不能正常工作。谁能帮我?

我想将参数存储在局部变量中

 DtaDisplay1 = ({match}) => {
    return (
      const id={match.params.pid};
      <div>
      <h1>{id}</h1>
    </div>)
}

在这里我传递url中的值:

<Route path="/dta/:pid" component={this.DtaDisplay1}/>
reactjs
3个回答
0
投票

您应该尝试在返回语句之前将id存储在变量中。

DtaDisplay1 = ({match}) => {
    const id=match.params.pid;
    return (
      <div>
      <h1>{id}</h1>
    </div>)
}

0
投票

你可以看到示例here。它将向您展示如何使用Rel路由器的url参数。

在此笔中,请参阅关于组件路径,关于链接和关于它自己的组件。我想你一定会从中得到解决方案。

const About = (props)=>{
  const id = props.match.params.aboutId;
  return <div>
    <h1>About - || id = {id}</h1>
    <p>This is about</p>
  </div>
}

0
投票

在所有代码片段中,我发现了一些或其他缺失的东西。请尝试使用以下代码段,看看它是否有效。这应该是正确的方法

DtaDisplay1 = ({match}) => {
    const id = match.params.pid;
    return (
      <div>
        <h1>{id}</h1>
      </div>
    );
}
© www.soinside.com 2019 - 2024. All rights reserved.