Http/observables在angular 2模块更新后现在不工作
Http调用在我重新安装/更新angular模块至最新版本之前起作用。现在,我发现所有Http调用/可观察对象现在都返回一个错误NOT FOUND(404[NOT FOUND])。请告诉我什么需要更新,我忘了什么吗?这是我的密码: NotificationService.ts:Http/observables在angular 2模块更新后现在不工作,angular,http,observable,Angular,Http,Observable,Http调用在我重新安装/更新angular模块至最新版本之前起作用。现在,我发现所有Http调用/可观察对象现在都返回一个错误NOT FOUND(404[NOT FOUND])。请告诉我什么需要更新,我忘了什么吗?这是我的密码: NotificationService.ts: import { Component, OnInit, Injectable } from '@angular/core'; import { Http, Response } from '@angular/http';
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();
}
}
开发依赖项:
"@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",
如果未在模块中定义提供程序,请在NotificationComponent.ts中添加提供程序 还导入可观察和HTTP_提供程序
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();
}
}
为什么要发出Http请求以获取应用程序中已经存在的数据?我将从中获取的RestfulAPI尚未准备好…那么我建议使用
MockBackend
来完成此操作,如中所示。这样,您就可以将假URL移出服务,当API确实存在时,更改的内容就更少了。@jornsharpe,谢谢您的回答。。。正是我需要的。