2017-05-10 3 views
0

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", 
+3

Warum machen Sie Http-Anfragen Daten zu erhalten, die bereits in Ihrer Anwendung? – jonrsharpe

+0

Die RestfulAPI, von der ich abholen werde, ist noch nicht fertig ... – vicgoyso

+1

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

Antwort

0

Wenn Sie nicht das Anbieter in der Definition Modul hinzufügen Provider in NotificationComponent.ts

auch importieren beobachtbar und HTTP_PROVIDERS

import { Component, Input, OnInit, OnDestroy } from '@angular/core'; 
import {Observable} from 'rxjs/Rx'; 
import {HTTP_PROVIDERS} from '@angular/http'; 
import { Notification, NotificationType } from './notification.entity'; 
import { NotificationService } from './notification.service'; 

@Component({ 
    selector: 'e-notifications', 
    templateUrl: './templates/notification.template.html', 
    providers : [HTTP_PROVIDERS, NotificationService] 
}) 

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(); 
    } 
}