그래서 Angular2를 배우기 시작했고 데이터베이스에있는 데이터를 가져 와서 내 indexpage에 표시하려고 시도하지만 매번 그렇게하려고합니다. "정의되지 않은 속성 '맵'을 읽을 수 없다는 오류가 발생하고 왜 시간을 검색했는지 전혀 알 수 없습니다. 누군가 나를 도울 수 있습니까?Angular2 Map "정의되지 않은 속성 '지도'를 읽을 수 없습니다.
import { Component } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { Injectable } from '@angular/core';
import { Bootcamp } from './Bootcamp';
const BOOTCAMP: Bootcamp[] = [];
@Injectable()
export class Bootcamptest {
private baseUrl: string = 'http://localhost:54220/Bootcampdata';
constructor(private http: Http) { }
getAll(): Observable<Bootcamp[]> {
let bootcamp$ = this.http
.get(`${this.baseUrl}/GetAlleBootcamps`, { headers: this.getHeaders() })
.map(mapBootcamps)
.catch(handleError);
return bootcamp$;
}
private getHeaders() {
let headers = new Headers();
headers.append('Accept', 'application/json');
return headers;
}
}
function mapBootcamps(response: Response): Bootcamp[] {
// uncomment to simulate error:
// throw new Error('ups! Force choke!');
// The response of the API has a results
// property with the actual results
return response.json().results.map(toBootcamp)
}
function toBootcamp(r: any): Bootcamp {
let bootcamp = <Bootcamp>({
id: extractId(r),
naam: r.name,
description: r.description,
begindatum: r.begindatum,
einddatum: r.einddatum
});
console.log('Parsed bootcamp:', bootcamp);
return bootcamp;
}
// to avoid breaking the rest of our app
// I extract the id from the person url
function extractId(bootcampData: any) {
let extractedId = bootcampData.url.replace('http://localhost:54220/Bootcampdata/Getallebootcamps', '').replace('/', '');
return parseInt(extractedId);
}
function mapBootcamp(response: Response): Bootcamp {
// toBootcamp looks just like in the previous example
return toBootcamp(response.json());
}
// this could also be a private method of the component class
function handleError(error: any) {
// log error
// could be something more sofisticated
let errorMsg = error.message || `Error met het laden van data!`
console.error(errorMsg);
// throw an application level error
return Observable.throw(errorMsg);
}
구성 요소 클래스는
import { Component, OnInit } from '@angular/core';
import { Bootcamp } from './Bootcamp';
import { Bootcamptest } from './Bootcamptest';
@Component({
selector: 'my-bootcamptest',
template: `
<section>
<section *ngIf="isLoading && !errorMessage">
Loading our hyperdrives!!! Retrieving data...
</section>
<ul>
<!-- this is the new syntax for ng-repeat -->
<li *ngFor="let person of people">
<a>
{{bootcamp.naam}}
</a>
</li>
</ul>
<section *ngIf="errorMessage">
{{errorMessage}}
</section>
</section>
`
})
export class Bootcamplistcomponent implements OnInit {
bootcamp: Bootcamp[] = [];
errorMessage: string = '';
isLoading: boolean = true;
constructor(private bootcamptest: Bootcamptest) { }
ngOnInit() {
this.bootcamptest
.getAll()
.subscribe(
/* happy path */ p => this.bootcamp = p,
/* error path */ e => this.errorMessage = e,
/* onComplete */() => this.isLoading = false);
}
}
어디에서 오류가 발생합니까? 적어도 파일이나 라인이 생기는 곳에서 제공해야합니다. 'getAll()'메소드가 아닌 것처럼 보입니다. – Supamiu
'mapBootcamps' 함수에서'response.json()'의 내용을 확인하십시오. –
'.get ('$ {this.baseUrl}/GetAlleBootcamps', {headers : this.getHeaders()})')가'null '인 것처럼 보입니다. 문제가되는 'DI'예외는 없습니다. – Brivvirs