observables가 단지 패턴으로 사용되는 대신 Angular 2에서 작동하는 방법에 대해 머리를 감싸려고합니다. 아래처럼 기본 관찰 변수를 생성하면 observable을 생성하고 소스 변수에 할당 한 결과로 존재하는 소스 변수에 subscribe 메소드를 호출 할 수 있습니다. 각도 2에서Observables는 Angular 2에서 어떻게 작동합니까?
let numbers = [19, 38, 57];
let source = Observable.from(numbers);
source.subscribe(
value => console.log(`value: ${value}`),
error => console.log(`error: ${error}`),
() => console.log('complete')
);
나는 다음과 같은 코드가 있습니다
구성 요소 :
private getDevicesByUserId(): void {
this.devicesHttp.getDevicesByUserId(this.userID)
.subscribe(
data => {
this.devices = data;
},
error => {},
() => {}
);
}
HTTP 서비스 :
public getDevicesByUserId(userID: number): Observable<any> {
return this.http.get(this.url + 'api/device/user/' + userID, {
headers: this.httpHeaders.getHeaders()
})
.map((response) => this.handleData(response))
.catch((error) => this.handleError(error));
}
을
위의 패턴에서 getDevicesByUserId 함수에 subscribe 메소드를 호출하고 있지만 http에는 관찰 가능이 작성되지 않았습니다. 어쩌면 내가 누락되었습니다하지만 http 호출 결과를 반환하는 함수입니다. 이와 같은 정규 함수에서 구독 메소드를 어떻게 호출 할 수 있습니까? Observable을 만들지 않으면 함수는이 속성을 어떻게 가지고 있습니까?
예, 감사합니다. – Aaron