服务文件
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 geturl = '/api/videos';
constructor(private _http:Http) { }
getvideos() {
return this._http.get(this.geturl).map((response:Response) => {
response.json()
});
}
}
这是显示此错误的订阅方法
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;
onselectvideo: Video;
switch: boolean = false
constructor(private videoserv: VideoService) {
//console.log(this.videos);
}
onselect(vid: any) {
this.switch = true;
this.onselectvideo = vid;
console.log(vid);
}
ngOnInit() {
this.videos = this.videoserv.getvideos .subscribe((response) => {
this.videos = response;
});
}
}
我有服务文件,我必须在其中调用我的 api 来获取 api,当我要订阅另一个类中的方法时,我正在调用该服务方法 getvideos() 然后它显示属性“订阅”的错误不存在于类型 ()=> observable
原文由 HASSAN MEHMOOD 发布,翻译遵循 CC BY-SA 4.0 许可协议
您没有调用
getVideos
方法。您在subscribe
getVideos
而不是返回值。调用subscribe
后调用getVideos()
: