reactjs - 在 nextjs 中使用 router.push 清理 url
问题描述
我有下面的代码。它重定向到 /home 并传递一个用户名变量。但是,在地址栏中,它显示 http://localhost:3000/home?username=bobmarley。如何使用 next.js 干净地传递变量?
import { withRouter } from 'next/router'
loginUser(this.state.user)
.then(response => {
var username = response['username'];
if (username) {
this.props.router.push({
pathname: '/home',
query: { username: username }
});
}
});
解决方案
使用router.pushas
的属性,这将是地址栏中显示的内容。将其设置为用于 的相同值。pathname
as
- 将在浏览器中显示的 URL 的可选装饰器。
this.props.router.push({
pathname: '/home',
as: '/home',
query: { username: username }
});
推荐阅读
- racket - 怎么设置!函数中对象的值?
- c# - 根据特定条件在请求正文中反序列化接口 asp.net core
- ruby-on-rails - 获取多态关联以通过验证
- python - 带有 s、d、b、c、o、x、X、n 等的 Python 索引......它们是什么?
- discord - 如何标记机器人的所有者
- elasticsearch - 仅对高于原点的匹配使用衰减函数
- c# - 如何从另一个类访问表单控件
- django - django.db.utils.IntegrityError:NOT NULL 约束失败:blog_article.created_on
- javascript - 为什么源映射包含“名称”?
- swift - 无法将文本文件放入 xcode 中的包中