【发布时间】:2018-08-11 01:53:42
【问题描述】:
我正在使用一个 API,我可以在其中使用帖子 ID 进行搜索。如何将链接更改为我创建的 slug。 Ex 而不是/posts/1,它将成为posts/slugtitle
Post.js
componentDidMount() {
const { match: { params } } = this.props;
axios
.get(`/api/posts/${params.postId}`)
.then(response => {
console.log(response);
this.setState({ post: response.data });
})
.catch(error => console.log(error));
}
Routes.js
<Route path={"/post/:postId"} strict component={PostShow} />
另外,如果没有搜索到的 id 的帖子,最简单的重定向方法是什么?
【问题讨论】:
-
蛞蝓从哪里来?您对 axios 的回应?
-
我不太确定我是否理解您想要实现的目标。要重定向,您可以使用将出现在
PostShow道具中的history对象。this.props.history.push('/somepath');或this.props.history.replace('/somepath'); -
是的,slug 在 axios 的响应中。