我正在升级我在Share Point办公室内运行的ANGLE应用程序,补充道.目前Angular 应用程序使用的是版本14,在我升级到Angular 15之后,它工作得很好.但一旦我升级到16岁,我就不会出错了.

TypeError: this._history.replaceState is not a function

这种情况发生在调用

 this.router.navigate(['/path_to_component']);

我在这篇文章上找到了一个解决方案.Office.js nullifies browser history functions breaking history usage

<script type="text/javascript">
    // Office js deletes window.history.pushState and window.history.replaceState. Cache them and restore them
    window._historyCache = {
        replaceState: window.history.replaceState,
        pushState: window.history.pushState
    };
</script>

<script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>

<script type="text/javascript">
    // Office js deletes window.history.pushState and window.history.replaceState. Restore them
    window.history.replaceState = window._historyCache.replaceState;
    window.history.pushState = window._historyCache.pushState;
</script>

我不确定这是如何在升级到Angular 16之后才被 destruct 的,尽管它与office.js有关

我使用的是同一版本的office e.js.

我已经使用了散列定位策略.有人能帮我理解这一点吗?

推荐答案

查看Angular 14/15‘S的源代码,我们可以看到BrowserPlatformLocation包含这样的方法:

override replaceState(state: any, title: string, url: string): void {
    if (supportsState()) {
      this._history.replaceState(state, title, url);
    } else {
      this.location.hash = url;
    }
  }

资料来源:

  1. https://github.com/angular/angular/blob/14.0.x/packages/common/src/location/platform_location.ts#L180C3-L186C4 (蛇形14 )
  2. https://github.com/angular/angular/blob/15.0.x/packages/common/src/location/platform_location.ts#L180C3-L186C4 (蛇15 )

你应该注意到的是supportsState():

export function supportsState(): boolean {
  return !!window.history.pushState;
}

在14/15Angular ,如果pushState不可用,则会出现后备情况,因此您的代码可以很好地工作,this._history.replaceState()也可以正常工作.

从16号角起,这张支票被删除了:

override replaceState(state: any, title: string, url: string): void {
    this._history.replaceState(state, title, url);
}

来源:https://github.com/angular/angular/blob/16.0.x/packages/common/src/location/platform_location.ts#L165

因此,因为office.js会删除pushStatereplaceState,并且没有备用用例,所以您的代码会中断(直到您使用已找到的修复程序).

Angular相关问答推荐

Angular前端调用ALB身份验证后的后端并重定向

Angularmaterial 中的处理mat—radio—button表单值访问器

Change上的Angular 自定义控件无法正常工作

带迭代的Angular 内容投影

向Anguar 17项目添加服务以启动和停止Loader-NGX-UI-Loader

角路径S异步旋转变压器仍可观察到

当我更新S显示的数据时,为什么我的垫表用户界面没有更新?

VS代码中带Angular 17的缩进新控制流

Angular将文件作为字符串读取给dxf解析器

ngRouterOutlet和组件生命周期使用自定义模板渲染的问题

Angular 获取视频持续时间并存储在外部变量中

检测父元素是否绑定到 Angular 中的子元素 @Output

Angular combinelatest使用没有初始值的主题

为什么 Angular2 (click) 事件没有在

Angular 2: Debounce(ngModelChange)?

无法获得 angular-material md-sidenav 的 100% 高度

设置 Angular Material Tooltip 的字体大小

angular2中的httpinterceptor类似功能是什么?

找不到@angular/common/http模块

Angular2:无法读取未定义的属性名称