angular2 - 无法从网络服务获取数据
angular2 - cannot get data from web service
第一次学习打字稿和angular2。我正在创建一个仅执行 GET 和 POST 的通用服务,以便我可以在整个应用程序中使用它。我的应用基于 Angular 来自 Dynamic Forms
的示例
我的问题是我的 "QuestionService" 使用的是 "ServerService" 但它抱怨 this.ServerService.getData is not a function
不是函数。
服务器服务
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class ServerService {
private apiUrl = 'app/users.json';
constructor (private http: Http) {}
getData (): Observable<any>[] {
return this.http.get(this.apiUrl)
.map(this.extractData)
.catch(this.handleError);
}
问题服务
import { ServerService } from './server.service';
@Injectable()
export class QuestionService implements OnInit{
errorMessage: string;
mode = 'Observable';
questions: QuestionBase<any>[];
ServerService = ServerService;
ngOnInit() { this.getQuestions(); }
getQuestions(ServerService: ServerService<any>){
console.log('getQuestions');
console.log(this.ServerService.getData());
this.ServerService.getData()
.subscribe(
questions => this.questions = questions,
error => this.errorMessage = <any>error);
}
你需要做的是让 Angular 将 ServerService
注入 ServerService
到 QuestionService
中,就像对 Http
在 ServerService
里面
@Injectable()
export class QuestionService implements OnInit{
constructor(private serverService: ServerService) {}
ngOnInit() { this.getQuestions(); }
getQuestions(){
this.serverService.getData()
.subscribe(
questions => this.questions = questions,
error => this.errorMessage = <any>error);
}
}
然后您需要将这两个服务添加到 providers
数组
@NgModule({
imports: [HttpModule],
providers: [ServerService, QuestionService]
})
export class AppModule {}
第一次学习打字稿和angular2。我正在创建一个仅执行 GET 和 POST 的通用服务,以便我可以在整个应用程序中使用它。我的应用基于 Angular 来自 Dynamic Forms
的示例我的问题是我的 "QuestionService" 使用的是 "ServerService" 但它抱怨 this.ServerService.getData is not a function
不是函数。
服务器服务
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class ServerService {
private apiUrl = 'app/users.json';
constructor (private http: Http) {}
getData (): Observable<any>[] {
return this.http.get(this.apiUrl)
.map(this.extractData)
.catch(this.handleError);
}
问题服务
import { ServerService } from './server.service';
@Injectable()
export class QuestionService implements OnInit{
errorMessage: string;
mode = 'Observable';
questions: QuestionBase<any>[];
ServerService = ServerService;
ngOnInit() { this.getQuestions(); }
getQuestions(ServerService: ServerService<any>){
console.log('getQuestions');
console.log(this.ServerService.getData());
this.ServerService.getData()
.subscribe(
questions => this.questions = questions,
error => this.errorMessage = <any>error);
}
你需要做的是让 Angular 将 ServerService
注入 ServerService
到 QuestionService
中,就像对 Http
在 ServerService
@Injectable()
export class QuestionService implements OnInit{
constructor(private serverService: ServerService) {}
ngOnInit() { this.getQuestions(); }
getQuestions(){
this.serverService.getData()
.subscribe(
questions => this.questions = questions,
error => this.errorMessage = <any>error);
}
}
然后您需要将这两个服务添加到 providers
数组
@NgModule({
imports: [HttpModule],
providers: [ServerService, QuestionService]
})
export class AppModule {}