Die Http Aufrufe funktionierte, bevor ich neu installiert/update die eckigen Module auf den neuesten. Jetzt habe ich entdeckt, dass alle HTTP-Aufrufe/Observables jetzt einen Fehler von NOT FOUND (http://localhost:4200/data/notifications.json 404 [Not Found]) zurückgeben. Bitte lassen Sie mich wissen, was aktualisiert werden muss, habe ich etwas vergessen? Hier ist mein Code:Http/Observables jetzt nicht nach angular 2 Module Update
NotificationService.ts:
import { Component, OnInit, Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class NotificationService {
constructor(private _http: Http){}
GetAllNotifications(queryUrl: string){
return this._http.get(queryUrl).map((response: Response) => response.json());
}
}
NotificationComponent.ts:
import { Component, Input, OnInit, OnDestroy } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Notification, NotificationType } from './notification.entity';
import { NotificationService } from './notification.service';
import 'rxjs/add/operator/map';
@Component({
selector: 'e-notifications',
templateUrl: './templates/notification.template.html'
})
export class NotificationComponent implements INotificationComponent {
notifType = NotificationType;
public notificationsCollection: Notification[];
private queryUrl = "../../../data/notifications.json";
notifSub: any;
constructor(private _notificationService: NotificationService) {
}
ngOnInit() {
this.notifSub = this._notificationService.GetAllNotifications(this.queryUrl)
.subscribe((response) => this.notificationsCollection = response);
}
ngOnDestroy() {
this.notifSub.unsubscribe();
}
}
Dev Abhängigkeiten:
"@angular/animations": "^4.1.1",
"@angular/cli": "^1.0.3",
"@angular/common": "^4.1.1",
"@angular/compiler": "^4.1.1",
"@angular/compiler-cli": "^4.1.1",
"@angular/core": "^4.1.1",
"@angular/forms": "^4.1.1",
"@angular/http": "^4.1.1",
"@angular/material": "^2.0.0-beta.3",
"@angular/platform-browser": "^4.1.1",
"@angular/platform-browser-dynamic": "^4.1.1",
"@angular/platform-server": "^4.1.1",
"@angular/router": "^4.1.1",
Warum machen Sie Http-Anfragen Daten zu erhalten, die bereits in Ihrer Anwendung? – jonrsharpe
Die RestfulAPI, von der ich abholen werde, ist noch nicht fertig ... – vicgoyso
Dann würde ich vorschlagen, 'MockBackend' zu verwenden, um dies zu tun, wie z. https://www.sitepoint.com/angular-2-mockbackend/, http://jasonwatmore.com/post/2016/11/24/angular-2-mockbackend-example-for-backendless-development. Auf diese Weise können Sie die gefälschten URLs aus Ihren Diensten entfernen und weniger ändern, wenn die API vorhanden ist. – jonrsharpe