在我的Angular 2项目中,ListingService无法通过post从服务器获取数据。我收到了这个错误:
EXCEPTION: TypeError: Cannot read property 'post' of undefined in [null]
ORIGINAL EXCEPTION: TypeError: Cannot read property 'post' of undefined
ORIGINAL STACKTRACE:
TypeError: Cannot read property 'post' of undefined
at ListingService.getListings (http://localhost:3000/app/listing.service.js:30:30)
at ListingsComponent.getListings (http://localhost:3000/app/listings.component.js:45:41)
at ListingsComponent.ngOnInit (http://localhost:3000/app/listings.component.js:41:26)
at AbstractChangeDetector.ChangeDetector_HostListingsComponent_0.detectChangesInRecordsInternal (eval at <anonymous> (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:10897:14), <anonymous>:22:99)
at AbstractChangeDetector.detectChangesInRecords (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:8824:14)
at AbstractChangeDetector.runDetectChanges (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:8807:12)
at AbstractChangeDetector._detectChangesContentChildren (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:8871:14)
at AbstractChangeDetector.runDetectChanges (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:8808:12)
at AbstractChangeDetector._detectChangesInViewChildren (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:8877:14)
at AbstractChangeDetector.runDetectChanges (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:8811:12)
我的ListingService.ts看起来像这样:
import {Injectable, Injector} from 'angular2/core';
import {HTTP_PROVIDERS, Http, Headers} from 'angular2/http';
import {Listing} from './listing';
import 'rxjs/add/operator/map';
@Injectable()
export class ListingService {
http: Http;
listings: Array<Listing>;
getListings() {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http.post('http://144.376.29.134:and-so-on', JSON.stringify(
{"id":1, "title": "Title", "description": "Проводите", "discount": "21%", "imageUrl": "http://lorempixel.com/400/200/sports", "tags": [{"tag": "Еда"}]
}),{headers:headers}
).map(res => res.json())
.subscribe((res:Array<Listing>) => this.listings = res);
return Promise.resolve(this.listings);
}
getListing(id: number) {
return Promise.resolve(this.listings)
.then(listings => listings.filter(l => l.id === id)[0]);
}
}
使用ListingService的ListingsComponent看起来像这样:
import {Component, OnInit} from 'angular2/core';
import {HTTP_PROVIDERS, Http, Headers} from 'angular2/http';
import {Listing} from './listing';
import {ListingService} from './listing.service';
import 'rxjs/add/operator/map';
@Component({
....
})
export class ListingsComponent implements OnInit {
listings: Array<Listing>;
constructor(private listingService:ListingService, private _router: Router) {}
ngOnInit() {
this.getListings();
}
getListings() {
this.listingService.getListings().then(listings => this.listings = listings);
}
getListing(id: number) {
return Promise.resolve(this.listings)
.then(listings => listings.filter(l => l.id === id)[0]);
}
gotoDetail(listing: Listing) {
this._router.navigate(['ListingDetail', { id: listing.id }]);
}
}
这可能是什么问题?
答案 0 :(得分:3)
您必须将HTTP_PROVIDERS
添加到组件providers
数组中,如下所示:
providers: [HTTP_PROVIDERS]
或者最好是在这样的引导程序中:
bootstrap(AppComponent, [HTTP_PROVIDERS]);
您错过了constructor
中的ListingService
http注入:
export class ListingService {
constructor(private http : Http){}
}
<强>附录强>
您没有收到任何列表的原因是因为您使用的是Promise而不是Observable:
getListings()
ListingService
中的返回此内容:
return this.http.post("bla bla").map(res => res.json).map((res) => {
return this.listings = res;
});
然后在getListings()
中的ListingsComponent
订阅
getListings() {
this.listingService.getListings().subscribe(listings => this.listings = listings);
}
答案 1 :(得分:0)
您应该从服务中的请求中返回observable对象:
@Injectable()
export class CompanyService {
constructor(http:Http/*, routeParams: RouteParams*/) {
this.http = http;
}
getCompanies() {
return this.http.get('https://angular2.apispark.net/v1/companies/')
.map(res => res.json());
}
}
从您的组件订阅一个监听器:
export class CompanyList implements OnInit {
public companies: Company[];
constructor(private service: CompanyService) {
this.service = service;
}
ngOnInit() {
this.service.getCompanies().subscribe(
data => this.companies = data);
}
}
希望它可以帮到你, 亨利