File for providing service:
import { Observable } from 'rxjs/Rx';
import { Http, Response} from '@angular/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/Map';
@Injectable()
export class VideoService {
private apiUrl = '/api/videos';
constructor(private _http:Http) { }
getVideos() {
return this._http.get(this.apiUrl).map((response:Response) => {
response.json()
});
}
}
Here is where the error occurs when using the subscribe method:
import { VideoService } from '../video.service';
import { Component, OnInit } from '@angular/core';
import { Video } from '../video';
import { Observable } from 'rxjs/Observable';
@Component({
selector: 'app-videocenter',
templateUrl: './videocenter.component.html',
styleUrls: ['./videocenter.component.css']
})
export class VideocenterComponent implements OnInit {
videos: any;
selectedVideo: Video;
showVideo: boolean = false;
constructor(private videoService: VideoService) {
}
onSelect(video: any) {
this.showVideo = true;
this.selectedVideo = video;
}
ngOnInit() {
this.videos = this.videoService.getVideos().subscribe((response) => {
this.videos = response;
});
}
}
In my service file, I make an API call to fetch videos. When I try to subscribe to this method in another class that calls the `getVideos()` method from the service, it throws an error saying that the property "subscribe" does not exist on type '()=>Observable'.