【问题标题】:TypeError: 'replaceState' called on an object that does not implement interface HistoryTypeError:在未实现接口 History 的对象上调用了“replaceState”
【发布时间】:2016-02-04 00:35:25
【问题描述】:

我正在查看这个 javascript 代码的 sn-p

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 == true

微软最新的东西给了我这个

调用对象无效

在 Chrome 中,同样的代码会抛出这个

未捕获的类型错误:非法调用

我在 Firefox 中收到此错误

TypeError:在未实现接口 History 的对象上调用了“replaceState”。

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

除了不同的错误信息,谁能告诉我这里发生了什么?

【问题讨论】:

    标签: javascript browser-history


    【解决方案1】:

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

    action.call(history,null, null, 
                window.location.pathname + window.location.search + '#' + hash);
    

    【讨论】:

      猜你喜欢
      • 2013-11-01
      • 1970-01-01
      • 2017-08-12
      • 2014-05-22
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多