Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React路由器v4-无法读取属性';参数';未定义的_Reactjs_React Router - Fatal编程技术网

Reactjs React路由器v4-无法读取属性';参数';未定义的

Reactjs React路由器v4-无法读取属性';参数';未定义的,reactjs,react-router,Reactjs,React Router,我是React的初学者。我只需要在加载页面时从URL获取路径参数。我的网址如下 http://localhost:3000/explorer/test/111 我已经设定了如下的路线 <Route exact path="/explorer/test/:id" component={() => <BlockPage />} /> 我犯了什么错误?有人能给我解释一下吗?我的目标是将URL中的111值转换为componentDidMount()当

我是React的初学者。我只需要在加载页面时从URL获取路径参数。我的网址如下

http://localhost:3000/explorer/test/111
我已经设定了如下的路线

<Route
    exact path="/explorer/test/:id"
    component={() => <BlockPage />}
  />

我犯了什么错误?有人能给我解释一下吗?我的目标是将URL中的111值转换为
componentDidMount()

当您想要使用组件时,您不需要像代码一样的函数,您可以像代码一样更改它:

<Route
exact path="/explorer/test/:id"
component={<BlockPage />}
/>

或者,如果需要这样的函数,请使用“渲染函数”

 <Route
exact path="/explorer/test/:id"
render={(props) => <BlockPage {...props} />}
/>
}
/>

然后在
this.props.match.params
中,您可以访问您的id。

当您想要使用组件时,您不需要像代码一样的功能,您可以像代码一样更改它:

<Route
exact path="/explorer/test/:id"
component={<BlockPage />}
/>

或者,如果需要这样的函数,请使用“渲染函数”

 <Route
exact path="/explorer/test/:id"
render={(props) => <BlockPage {...props} />}
/>
}
/>

然后在
this.props.match.params
中,您可以访问您的id。

发生这种情况是因为您知道该组件是其他组件的子组件。比如说,

<BrowserRouter>
        <Switch>
            <Route path="/Feedback">
        <Switch>
<BrowserRouter>


在上面的路由定义中,Feedback组件中的match对象不为null,但如果我们在Feedback中有其他组件并尝试访问它,则这是一个错误

发生这种情况是因为您知道该组件是其他组件的子组件。比如说,

<BrowserRouter>
        <Switch>
            <Route path="/Feedback">
        <Switch>
<BrowserRouter>

在上面的路由定义中,Feedback组件中的match对象不为null,但如果我们在Feedback中有其他组件并尝试访问它,则这是一个错误

这个很好用。。。试试这个

<Route
exact path="/explorer/test/:id"
render={(props) => <BlockPage {...props} />}
/>
}
/>
这个很好用。。。试试这个

<Route
exact path="/explorer/test/:id"
render={(props) => <BlockPage {...props} />}
/>
}
/>

这解决了我的问题:

<CPAroute exact path="/advcampaigns/:id" component={AdvCampaigns}/>

这解决了我的问题:

<CPAroute exact path="/advcampaigns/:id" component={AdvCampaigns}/>

你能给我解释一下吗。我像你提到的第二种方法一样改变了路线,但对我来说仍然是相同的错误。(TypeError:无法读取未定义的属性'params')我如何访问componentDidMount()中的:id?@sajitaliYanage您是否使用我说的
render
函数?@sajitaliYanage我的下一个问题是您是否
console.log(this.props)
在console中看到了什么?请在
BlockPage componentDidMount
@sajitaliYanage中插入这一行,如果您使用路由器删除
,然后只导出该组件,您可以解释一下吗。我像你提到的第二种方法一样改变了路线,但对我来说仍然是相同的错误。(TypeError:无法读取未定义的属性'params')我如何访问componentDidMount()中的:id?@sajitaliYanage您是否使用我说的
render
函数?@sajitaliYanage我的下一个问题是您是否
console.log(this.props)
在console中看到了什么?请在
BlockPage componentDidMount
@SajithaLiyanage中插入这一行,如果用路由器删除
,然后只导出该组件,该怎么办