首页 > 解决方案 > history.push 替换最后一条路径

问题描述

(同样适用于 react-router history.push

据我所知,如果我的 URL 不以 开头/,那么它会附加到当前路径。

因此,例如,如果我当前的路径是https://google.com并且如果我打电话history.pushState({},'','/test-path'),那么 iI 将被定向到https://google.com/test-path.

但是,对于我的路径不以 结尾的情况/,它会用参数替换最后一位。

例如,如果我当前的路径是https://google.com/abc, 和 call history.pushState({},'','test-path'),那么我也会被定向到https://google.com/test-path

这是预期的行为吗?如果我想移至https://google.com/abc/test-pathfrom https://google.com/abc,以下代码是实现此目的的最佳选择吗?

history.pushState({},'',location.pathname + '/test-path')

标签: react-routerbrowser-historypushstate

解决方案


推荐阅读