属性'subscribe'在类型'()=>上不存在.可观察的“任何" [英] Property 'subscribe' does not exist on type '() => Observable<any>'
问题描述
服务文件
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()
});
}
}
在此处显示此错误的订阅方法
here is where the subscribe method showing this error
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(),然后显示该属性的错误(subscribe)在()=>可观察类型上不存在
i have service file in which i have to call my api to get the api's and when i am going to subscribe the method in the other class where i am calling that service method getvideos() then its showing the error that the property "subscribe " does not exist on type ()=> observable
推荐答案
您没有调用getVideos
方法.您正在调用getVideos
的函数引用上的subscribe
,而不是返回值.呼叫getVideos()
后呼叫subscribe
:
You are not calling the getVideos
method. You are calling subscribe
on the function reference of getVideos
and not the returned value. Call subscribe
after you call getVideos()
:
ngOnInit() {
this.videoserv.getvideos().subscribe((response) => {
this.videos = response
});
}
这篇关于属性'subscribe'在类型'()=>上不存在.可观察的“任何"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!