Reactjs - 处理程序路由器访问
Reactjs - Handler router access
我从 reactjs
开始使用 reduxjs
和 react-router-dom
我有一些路由,例如 main/route-1
、main/route-2
... 我想调用一些 api 以在用户 access
或 refresh
。有什么方法可以处理访问路由器 url 以使用该路由器调用特殊 api 吗?最好的方法是什么谢谢。
我不知道有什么方法不能通过路由器呼叫 API。但是你可以在组件加载时调用API。使用 axios
在 componentWillMount()
函数中调用 API。
编辑:在我再次查看 documantation 后,他们提到 componentWillMount
方法被认为是遗留的并且不使用它。所以你不能使用 componentDidMount
它会在组件安装后立即调用 API 。他们的文档还说 componentDidMount 是实例化网络请求的好地方。
我从 reactjs
开始使用 reduxjs
和 react-router-dom
我有一些路由,例如 main/route-1
、main/route-2
... 我想调用一些 api 以在用户 access
或 refresh
。有什么方法可以处理访问路由器 url 以使用该路由器调用特殊 api 吗?最好的方法是什么谢谢。
我不知道有什么方法不能通过路由器呼叫 API。但是你可以在组件加载时调用API。使用 axios
在 componentWillMount()
函数中调用 API。
编辑:在我再次查看 documantation 后,他们提到 componentWillMount
方法被认为是遗留的并且不使用它。所以你不能使用 componentDidMount
它会在组件安装后立即调用 API 。他们的文档还说 componentDidMount 是实例化网络请求的好地方。