我有一个运行良好的 API,但是当我将它与 react 一起使用时,get 请求转到
/api/profile/posts/profile/matt
而它应该去:
/api/posts/profile/matt
代理设置localhost:8000/api为 ,当使用 react 时,它适用于其他 API。
调用 API 的代码(只有配置文件一个不起作用,其余一切正常,即使配置文件在我使用绝对 URL 时也能工作):
const res = username
? await axios.get("posts/profile/"+username)
: await axios.get("posts/timeline/6120b0e07ea0361eb4982b1c");
路线代码:
function App() {
return (
<Router>
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/login">
<Login />
</Route>
<Route path="/register">
<Register />
</Route>
<Route path="/profile/:username">
<Profile />
</Route>
</Switch>
</Router>
)
}
我和这个用户也有类似的问题:如何防止 React-router 弄乱你的 API url