【发布时间】:2018-07-12 05:39:43
【问题描述】:
我正在尝试在 Angular 5 中构建一个数据服务,该服务从平面文件中读取数据并将其传递给组件进行显示。该文件位于相对于数据服务的 ../data-files 目录中。
问题是我很难验证服务是否正在向组件提供数据。为了简单起见,我尝试放置一个 console.log(data);在组件的构造函数中,但到目前为止我一直无法确定发生了什么。
这是我的数据服务代码:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
@Injectable()
export class DataService {
dataUrl = '../data-files/migrations.json';
DATA;
constructor(private http: HttpClient) {};
getData(): Observable<any>{
return this.http.get(this.dataUrl);
};
ngOnInit() {
this.getData();
};
}
这就是我的组件的样子:
import { Component, OnInit, OnChanges, ViewChild, ElementRef, Input, ViewEncapsulation } from '@angular/core';
import * as d3 from 'd3';
import { DataService } from '../services/data.service';
@Component({
selector: 'app-mosaic-view',
templateUrl: './mosaic-view.component.html',
styleUrls: ['./mosaic-view.component.css'],
encapsulation: ViewEncapsulation.None,
// providers: [DataService]
})
export class MosaicViewComponent implements OnInit, OnChanges {
@ViewChild('chart') private chartContainer: ElementRef;
// @Input() private data: Array<any>;
data;
// chart variables go here.
private chart: any;
private width: number;
private height: number;
constructor(private dataService: DataService) {
console.log("mosaic-view constructed..."); // does not print anything.
};
ngOnInit() {
this.getData();
this.createChart();
if(this.data){
this.updateChart();
}
};
ngOnChanges() {
if(this.chart){
this.updateChart();
}
};
getData(): void {
this.data = this.dataService.getData().subscribe(data => this.data = data);
console.log(this.data); // does not print anything.
}
createChart(){
// creates the mosaic viz, build on d3.treemap()
// TODO
};
updateChart(){
// updates the viz when the data changes.
// TODO
};
}
console.log(...) 语句从不打印,即使我将它们直接放在构造函数中。
在我的 app.component.html 文件中,我有一个对选择器的引用,如下所示:
...
<div id="app-mosaic-view"></div>
...
在 linter 或控制台中没有显示错误。我已尝试遵循设置此类服务的最佳实践,但有些地方不对劲,我无法确切说明是什么。
我们将不胜感激。
编辑:我做了一些建议的更改,但现在我遇到了我尝试加载的数据文件的 404 错误。我检查了路径,甚至尝试明确指定路径无济于事。知道为什么找不到文件吗?
新组件:
import { Component, OnInit, OnChanges, ViewChild, ElementRef, Input, ViewEncapsulation } from '@angular/core';
import * as d3 from 'd3';
import { DataService } from '../services/data.service';
@Component({
selector: 'app-mosaic-view',
templateUrl: './mosaic-view.component.html',
styleUrls: ['./mosaic-view.component.css'],
encapsulation: ViewEncapsulation.None,
// providers: [DataService]
})
export class MosaicViewComponent implements OnInit, OnChanges {
@ViewChild('chart') private chartContainer: ElementRef;
componentName: string = 'Mosaic View';
private data: any[];
private errorMessage: string;
// chart variables go here.
private chart: any;
private width: number;
private height: number;
constructor(private dataService: DataService) {
console.log("mosaic-view constructed...");
};
ngOnInit() {
this.getData();
if(this.data){
this.updateChart();
}
};
ngOnChanges() {
if(this.chart){
this.updateChart();
}
};
getData(): void {
this.dataService.getData().subscribe(
(data: any[]) => {
// this.data = data;
this.data = data;
this.createChart(data);
},
(error: any) => this.errorMessage = <any>error
);
console.log(this.data);
}
createChart(data){
// creates the mosaic viz, build on d3.treemap()
// TODO
};
updateChart(){
// updates the viz when the data changes.
// TODO
};
}
新服务:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import {catchError, map, tap } from 'rxjs/operators';
@Injectable()
export class DataService {
dataUrl: string = '../consolidated_cohorts/src/app/data-files/migrations.json';
constructor(private http: HttpClient) {
console.log("data service called...")
this.getData();
};
getData(): Observable<any>{
return this.http.get<any[]>(this.dataUrl)
.pipe(
tap(data => console.log(data)),
// catchError(this.handleError) // TODO
);
};
// private handleError: {
// console.log("there was an error.");
// }
}
404 错误: http://localhost:4200/data-files/migrations.json404(未找到)
【问题讨论】: