Angular 财产';订阅';不存在于类型';()=>;可观察<;任何>';
服务文件Angular 财产';订阅';不存在于类型';()=>;可观察<;任何>';,angular,typescript,rxjs,observable,angular2-services,Angular,Typescript,Rxjs,Observable,Angular2 Services,服务文件 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'; con
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()
});
}
}
下面是显示此错误的subscribe方法的位置
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”不存在于type()=>observable您没有调用
getvideos
方法。您正在调用getVideos
的函数引用上的subscribe
,而不是返回值。调用getVideos()
后,调用subscribe
:
您应该在videoserv
服务上调用getvideos()
方法。
您缺少了
()
,getvideos
是一个方法而不是属性。但是如果您还想在应用程序的其他位置(组件、管道等)使用服务方法getvideos()
您可以使用.pipe(map())代替.subscribe()
然后使用。在您想要播放视频的地方订阅
this.videoserv.getvideos().subscribe(response) => {
this.videos2 = response
});
另外,
开关
是一个受保护的字。您将无法将其用作变量名。我得到的属性“map”在类型“string”上不存在。在这里:返回this.\u http.get(this.geturl).map((response:response)=>{response.json()});
ngOnInit() {
this.videoserv.getvideos().subscribe((response) => {
this.videos = response
});
}
ngOnInit() {
this.videoserv.getvideos().pipe(map(response) => {
this.videos = response
}));
}
this.videoserv.getvideos().subscribe(response) => {
this.videos2 = response
});