【问题标题】:removing params in url for nextjs redirect删除 url 中的参数以进行 nextjs 重定向
【发布时间】:2021-07-02 14:56:45
【问题描述】:

我有下面的代码。它重定向到 /home 并传递一个电子邮件变量。但是,在地址栏中,它显示 http://localhost:4000/home?email=steverodgers@gmail.com。如何干净利落地使用 react 和 next.js 传递变量?

import { withRouter } from 'next/router'
authenticateUser(this.user)
        .then(response => {
          var email = response['email'];

          if (email) {
            this.props.router.push({
              pathname: '/home',
              query: { email: email}
            });
          }
      });

【问题讨论】:

    标签: reactjs routes react-router next.js push


    【解决方案1】:

    使用您的示例电子邮件参数,您必须设置 dynamic routing 并将您的 router to push 配置为电子邮件地址。

    如果您启用浅层路由,请知道它仅适用于same page URL。设置动态页面后,您可以通过 router.push 导航到它,例如:

    router.push('home/[email]', `/home/${email}`);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-07
      • 2022-12-18
      • 2016-07-19
      • 2019-09-11
      • 2014-12-10
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      相关资源
      最近更新 更多