【问题标题】:Component/Service, change detection and zone组件/服务、变更检测和区域
【发布时间】:2016-08-25 15:21:31
【问题描述】:

我实际上遇到了区域问题。我有一个组件,在一个名为 App 的 ngmodule 中,从另一个名为“Dossier”(App 导入 Dossier)的 ngmodule 调用另一个名为“tache-list”的组件。 我发送一个带有 TacheService 的 http 请求,返回我的 taches(任务),我想用我的 TacheListComponent 打印它们。但是,当页面加载时,没有变化检测。我做了很多研究,发现了一些关于 zone 和 Angular Zone 的文档。我尝试使用 NgZone.run(),这次检测到了 taches 的变化。 这怎么可能?有没有比在我的所有服务功能中粘贴运行功能更好的方法?谢谢您的回复,关于角度区域的信息似乎很少......

tache-list.component.ts :

import { Component, OnInit }      from '@angular/core';
import { TacheService } from '../services/tache.service';
import { Tache } from '../entities/tache.ts';
import { ChangeDetectorRef } from "@angular/core";
import 'rxjs/Observable';
import 'rxjs/add/operator/toPromise'
@Component({ 
    selector: 'tache-list',
    templateUrl: 'build/dossier/partials/tache-list.component.html',
    styleUrls: ['build/dossier/css/tache-list.component.css']
})
export class TacheListComponent implements OnInit {
    title: String = 'Liste des tâches';
    taches: Object[];
    errorMessage: any;
    subscriptions: any[] = [];
    constructor(private tacheService: TacheService, private changeDetector: ChangeDetectorRef) { }

    ngOnInit() {
        this.getTachesDuJour();
    }
    ngOnDestroy() {
        this.subscriptions.forEach(sub => {
            sub.unsubscribe();
        });
    }

    getTachesDuJour() {
        return this.tacheService.getTachesDuJour().subscribe(
            taches => {
                this.taches = taches;
                //this.changeDetector.detectChanges(); 
            },
            error => {
                this.errorMessage = <any>error;
            }
        );
    }
}

tache.service.ts:

import { Injectable, OnInit, NgZone } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable }     from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class TacheService implements OnInit {
    url: string = 'http://web-pierre/ebexapplication/public/api/tache';

    constructor(private http: Http, private zone: NgZone) { }
    ngOnInit() {

    }

    private extractData(res: Response) {
        let body = res.json();
        return body || {};
    }
    private handleError(error: any) {
        let errMsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error';
        console.error(errMsg);
        return Observable.throw(errMsg);
    }

    getTachesDuJour(): Observable<Object[]> {
        this.zone.run(() => {});
        var response = this.http.get(this.url).map(res => this.extractData(res)).catch(this.handleError);
        return response;
    } 
}

【问题讨论】:

  • 如果您不希望它出现在您的服务功能中,您可以改为在订阅的下一个处理程序中进行。您是在网络、桌面还是移动设备上执行此操作?
  • 在网络中。 subscribe 的下一个处理程序是什么意思?在我的组件中?
  • 是的,在您的组件中。 taches => this.zone.run(() => this.taches = taches); Obserables 具有三个可以提供给订阅的函数:onNext、onError、onComplete 按顺序排列。以下是要查看的资源:@​​987654321@
  • 您显示的代码应该可以工作,无需调用zone.run()。您是否在应用程序的某处使用OnPush 更改检测策略?你能在plunker 中重现这个问题吗?
  • 发现了问题,我加载的 zone.js 文件顺序错误。太笨了……谢谢你的回答

标签: angular


【解决方案1】:

发现问题,我加载 zone.js 文件的顺序不正确。太蠢了……谢谢你的回答。

【讨论】:

    猜你喜欢
    • 2014-08-30
    • 1970-01-01
    • 2017-08-26
    • 2017-12-31
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    相关资源
    最近更新 更多