天天看点

react修改URL不刷新页面

有一种情况,不得不将参数放在URL上,但是又不想让别人看到参数,所以能获取到数据后将参数去掉又不影响路由的正常跳转,而且不刷新页面,特别是不能刷新两次。

  • location.href 如果参数在路由的后面,可以用这种办法。
  • window.history.pushState({}, 0, 新的地址);如果参数在路由的前面,则用该方法,用location.href的话会刷新两次。
const href = window.location.origin + window.location.hash;
window.history.pushState({}, 0, href);