我使用Nodejs后端返回OAuth的自定义重定向URL。我试图让React将用户重定向到这个URL,但是它在前面添加了localhost。
import React, { Component } from "react";
import { withRouter } from "react-router";
import axios from "axios";
class Test extends Component {
async componentDidMount() {
const profileData = await axios.get("/connect");
this.props.history.push(profileData.data);
}
render() {
return <div />;
}
}
export default withRouter(Test);因此,假设返回的profileData.data字符串为http://www.google.com,则网页将重定向到http://localhost:3000/http://www.google.com。
有没有办法用React重定向,让它重定向到一个特定的URL,而不是尝试重定向到一个子页面?
发布于 2019-02-22 15:05:23
只需在componentDidMount函数中设置位置href即可。我会这样写:
componentDidMount() {
axios.get("/connect")
.then((response) => {
window.location.href = response.data
})
}https://stackoverflow.com/questions/54820281
复制相似问题