firefox

时间:2017-01-27 15:00:31

标签: angular typescript

我在firefox和所有ios浏览器上使用Angular 2应用程序时遇到了非常奇怪的问题(firefox,safari)

当用户在我的应用程序中输入/reports路径时,我通过ngOnInit方法调用我的rest api来获取json数据。

这是我处理数据加载的ngOnInit方法的一部分:

ngOnInit() {
//this.service is my service to get reports
    this.service.getReports(this.categoryId).subscribe(res => {
        this.testReports = res;
    }
}

我收到回复后,我在html文件中呈现报告:

<div class="cat-item-holder" *ngFor="let singleCategory of testReports; let i=index">
{{singleCategory.name}}
</div>

在Chrome中,我可以看到所有报告在我输入/reports页面后立即可见。在firefox和safari中,我可以看到应用程序已经挂断了#39;我必须在页面的某处双击三次以查看我的api的结果。

有谁知道如何解决这个问题?也许这是ngOnInit方法的问题?

我的angular-cli.json文件:

{
  "project": {
    "version": "1.0.0-beta.22-1",
    "name": "APPNAME"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico",
        "assets/config/config_google.json",
        "../backend/web/uploads"
      ],
      "index": "index.html",
      "main": "main-polymer.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "mobile": false,
      "styles": [
        "../node_modules/bootstrap/dist/css/bootstrap.min.css",
        "assets/css/styles.css"
      ],
      "scripts": [
         "assets/bower_components/webcomponentsjs/webcomponents.min.js",
         "../node_modules/core-js/client/shim.min.js"
      ],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": true,
    "inline": {
      "style": true,
      "template": false
    },
    "spec": {
      "class": true,
      "component": true,
      "directive": true,
      "module": true,
      "pipe": true,
      "service": true
    }
  }
}

- 我的包json文件

{
  "name": "front",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "start": "ng serve",
    "lint": "tslint \"src/**/*.ts\"",
    "test": "ng test",
    "pree2e": "webdriver-manager update",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "2.2.3",
    "@angular/compiler": "2.2.3",
    "@angular/core": "2.2.3",
    "@angular/forms": "2.2.3",
    "@angular/http": "2.2.3",
    "@angular/material": "^2.0.0-alpha.11-3",
    "@angular/platform-browser": "2.2.3",
    "@angular/platform-browser-dynamic": "2.2.3",
    "@angular/router": "3.2.3",
    "@angular/upgrade": "~2.2.1",
    "@vaadin/angular2-polymer": "^1.0.0-rc1",
    "angular2-fontawesome": "~0.7.0",
    "angular2-in-memory-web-api": "0.0.21",
    "angular2-jwt": "^0.1.26",
    "bootstrap": "^3.3.7",
    "core-js": "^2.4.1",
    "ie-shim": "^0.1.0",
    "ng-disable-scroll": "^0.1.1",
    "ng2-bootstrap": "^1.1.16",
    "ng2-dropdown": "0.0.15",
    "ng2-filter-pipe": "^0.1.4",
    "ng2-page-scroll": "3.2.3",
    "ng2-parallax": "^0.3.0",
    "ng2-parallax-scroll": "^0.4.0",
    "ng2-sticky-kit": "^1.1.0",
    "rxjs": "5.0.0-beta.12",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.6.23"
  },
  "devDependencies": {
    "@angular/compiler-cli": "2.2.3",
    "@types/jasmine": "2.5.38",
    "@types/node": "^6.0.42",
    "angular-cli": "1.0.0-beta.21",
    "codelyzer": "~2.0.0-beta.1",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "4.0.9",
    "ts-node": "1.2.1",
    "tslint": "^4.0.2",
    "typescript": "~2.0.3",
    "webdriver-manager": "10.2.5"
  }
}

和我的报告服务实施:

import {Injectable } from '@angular/core';
import {Http, Headers, Response , URLSearchParams} from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { MainService } from './main.service';

@Injectable()
export class ReportsService {
   constructor(private http:Http , private mainService:MainService) {
    this.http = http;
  }

  getReports(categoryId) : Observable<Response> {
    let params: URLSearchParams = new URLSearchParams();
    params.set('cat', categoryId);
    params.set('user', this.mainService.getMainUserMail());
    return this.http.get(this.mainService.getServiceUrl()+'/reports/' , {headers: this.mainService.getHeaders() , search: params}).map(res => res.json());
  }
}

在firefox的我的报告页面中,我可以在加载部分reports page的右上角看到小滚动,如果我在此滚动上单击几次,报告正在Firefox中正确呈现,也许这是一些与DOM相关的问题?

好的,我已经找到了解决此问题的方法,在app.component.ts我已添加

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

export class AppComponent {

    constructor(private _applicationRef: ApplicationRef, private _router: Router) {
        setInterval(() => {
              this._applicationRef.tick();
           }, 5000);
    }

}

reports.ts组件中:

 constructor(private ref: ChangeDetectorRef) {
        setInterval(() => {
           this.ref.detectChanges();
         }, 5000);
}

现在在firefox中加载5秒后,我几乎没有挂断浏览器,并且成功呈现了结果,但有人知道如何防止这种挂断吗?

1 个答案:

答案 0 :(得分:0)

将其作为“回答”发布,但它更多是基于您自己的解决方法的解决方法。我仍然认为问题是polymer与角度变化检测相结合。

@Component({...})
export class ReportsComponent {

    constructor(private changeDetector: ChangeDetectorRef){}

    ngOnInit() {
        this.service.getReports(this.categoryId).subscribe(res => {
            this.testReports = res;
            setTimeout(() => {
                this.changeDetector.detectChanges();
            });
        }
    }
}

不完全确定setTimeout是否有必要,但那是你要找出来的。这将触发对此组件及其子组件的更改检测。基本上你用鼠标点击和鼠标移动做的事情就是触发角度的变化检测。