带有自定义XHR polyfill的角度不起作用,因为“ zoneJS的外部”

问题描述 投票:0回答:1

问题是使用zoneJS角形检测器制作一个自定义XHR polyfill。

历史:

我正在使用Ionic 4,有角度。这时我已经警告过uiwebview api的弃用,所以我尝试添加cordova-plugin-wkwebview-engine,但是由于我使用了一些本地文件(file://),因此插件不喜欢(某些部分坏了) ),因此我安装了cordova-plugin-wkwebview-file-xhr,但与此同时,我有:

Navigation triggered outside angular zone, did you forget to call ngZone?

我认为问题是因为cordova-plugin-wkwebview-file-xhr向de XMLHttpRequest添加/修改了海关代码,而zoneJs检测丢失了。

所以我如何使其与海关XMLHttpRequest一起使用zoneJS?我可以重新检测吗?

有人在这里问一个类似的问题:https://github.com/oracle/cordova-plugin-wkwebview-file-xhr/issues/52,但我认为这个问题更多是关于使用自定义代码的angular zoneJS

angular ionic-framework cordova-plugins zonejs
1个回答
0
投票

通常在这种情况下,您有2个选择:

  1. 收到来自您的“自定义” XHR的响应后触发更改检测:

https://angular.io/api/core/ChangeDetectorRef

在您的ts中:

import { ChangeDetectorRef } from '@angular/core';

...

public data;

constructor(private cdr: ChangeDetectorRef) {
}
...

callToServer() {
    this.httpService.getData().subscribe((data)=>{
        this.data = data;
        this.cdr.detectChanges();
    })
};
  1. 或者您可以通过使用回调内的区域来运行将其置于Angular区域内的方法:

https://angular.io/api/core/NgZone

在您的ts中:

import { NgZone } from '@angular/core';

...

public data;

constructor(private zone: NgZone) {
}
...

callToServer() {
    this.httpService.getData().subscribe((data)=>{
        this.zone.run(() => {
            this.data = data;
        })
    })
};
© www.soinside.com 2019 - 2024. All rights reserved.