【发布时间】:2016-10-26 20:15:00
【问题描述】:
如果我在资源 /people 上对我的 api 执行以下 http 调用并返回
{
"_embedded": {
"people": [
{
"id": 1,
"firstName": "John",
"lastName": "Doe",
"_links": {
"self": {
"href": "http://localhost:8080/person/1"
},
"person": {
"href": "http://localhost:8080/person/1"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/person"
}
}
}
数据正在屏幕上的表格中呈现,目前我从后端公开我的 ID,并使用以下链接让我的 react-router 处理
<Link to={`person/${this.props.person.id}`}>Detail</Link>
在这里,我无法在呈现的链接中传递链接,而必须传递 id 才能在我的后端进行详细调用。但是我不想手动构建href,而是想在我的下一个组件中使用_links.self.href 的url。
如何在我的 React 应用程序中不暴露 id 来实现这一点?
【问题讨论】:
-
I can't pass it as a parameter since it's a link你能扩展一下吗?另外,你是怎么路由的?反应路由器? -
你说得对,不清楚,现在好些了吗?
-
更清楚了,谢谢。但是
<Link to={${this.props.person._links.self.href}}>Detail</Link>不会做你想做的事吗?我假设person是您 API 响应中该数组的结构。 -
不,json是从我的api返回的,
href中的值是我后端的位置。
标签: javascript reactjs hateoas