TypeError: 'replaceState' called on an object that does not implement interface History

TypeError: 'replaceState' called on an object that does not implement interface History

我正在查看这段 javascript 代码

if (history) {
    var action = settings.replaceState ? history.replaceState : history.pushState;
    if (action) {
        // next line throws the error
        action(null, null, window.location.pathname + window.location.search + '#' + hash);
    }
}

settings.replaceState == 真

Microsoft 的最新产品给了我这个

Invalid calling object

在 Chrome 中,同一段代码抛出此

Uncaught TypeError: Illegal invocation

我在 Firefox 中收到此错误

TypeError: 'replaceState' called on an object that does not implement interface History.

当我调试历史记录时,它看起来应该是这样,并且有一个包含此方法的原型。

除了不同的错误消息之外,谁能告诉我这是怎么回事?

通过将方法分配给变量,您丢失了 history 的执行上下文。您需要使用 call/bind

将执行上下文设置回 history
action.call(history,null, null, 
            window.location.pathname + window.location.search + '#' + hash);