react 用fetch 登录,成功后怎么跳转组件

发布于 2022-09-06 22:55:53 字数 952 浏览 8 评论 0

1.登录成功后,后台返回的数据到fetch的data里,然后怎么用路由跳转到其他组件呢
2.如果不用路由,可以用window.location.href来跳转到组件吗
handleSubmit = (e) => {

    e.preventDefault();
    // let history = this.context.router.history;
    this.props.form.validateFields((err, values) => {
        if (!err) {
            fetch('/api/login',{
                method:'POST',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type':'application/json;charset=UTF-8'
                },
                body:JSON.stringify({'email':values.email,'password':values.password}),
            }).then(function (res) {
                return res.json();
            }).then(function(data){
                console.log(data)
                // window.location.href='/usercenter';
             
                }
            ).catch(function (err) {
                console.log(err);
            })
        }
    });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

划一舟意中人 2022-09-13 22:55:53

如果不使用路由,你可以把要跳转的组件引入进来,然后把当前的组件替换掉
fetch成功之后那数据放到state里面,然后在render下面if判断一下根据数据来显示不同的组件

我很坚强 2022-09-13 22:55:53
    import { withRouter } from 'react-router-dom';  // 相对react-router之前版本,react-router4通过withRouter获取history,否则拿不到history
    this.props.history.push('/usercenter');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文