属性 'subscribe' 在类型 '() => Observable<any>' 上不存在
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()
});
}
}
这里是显示此错误的订阅方法
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,并且当我要订阅另一个 class 中的方法时,我在哪里我正在调用该服务方法 getvideos() 然后它显示 属性 "subscribe " 在类型 ()=> observable
上不存在的错误
您没有调用 getVideos
方法。您正在对 getVideos
的函数引用而不是返回值调用 subscribe
。调用 getVideos()
后调用 subscribe
:
ngOnInit() {
this.videoserv.getvideos().subscribe((response) => {
this.videos = response
});
}
ngOnInit() {
this.videoserv.getvideos().subscribe((response) => {
this.videos = response
});
}
您应该在 videoserv
服务上调用 getvideos()
方法。
您错过了 ()
,getvideos
是一种方法,而不是 属性。
但是如果您还想在应用程序的另一个地方(组件、管道...等)使用您的服务方法 getvideos()
你可以做 .pipe(map()) 而不是 .subscribe()
ngOnInit() {
this.videoserv.getvideos().pipe(map(response) => {
this.videos = response
}));
}
然后在你想要视频的地方使用.subscribe
this.videoserv.getvideos().subscribe(response) => {
this.videos2 = response
});
this.service.method().subscribe(response)...
每当输入 this.service
出现自动建议时,在 suggestion 中,建议方法名,一旦接受建议,它只占用不带括号的方法名()。
this.service.method.subscribe()..
=> 会报错
this.service.method().subscibe()..
=> 正确
服务文件
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,并且当我要订阅另一个 class 中的方法时,我在哪里我正在调用该服务方法 getvideos() 然后它显示 属性 "subscribe " 在类型 ()=> observable
上不存在的错误您没有调用 getVideos
方法。您正在对 getVideos
的函数引用而不是返回值调用 subscribe
。调用 getVideos()
后调用 subscribe
:
ngOnInit() {
this.videoserv.getvideos().subscribe((response) => {
this.videos = response
});
}
ngOnInit() {
this.videoserv.getvideos().subscribe((response) => {
this.videos = response
});
}
您应该在 videoserv
服务上调用 getvideos()
方法。
您错过了 ()
,getvideos
是一种方法,而不是 属性。
但是如果您还想在应用程序的另一个地方(组件、管道...等)使用您的服务方法 getvideos()
你可以做 .pipe(map()) 而不是 .subscribe()
ngOnInit() {
this.videoserv.getvideos().pipe(map(response) => {
this.videos = response
}));
}
然后在你想要视频的地方使用.subscribe
this.videoserv.getvideos().subscribe(response) => {
this.videos2 = response
});
this.service.method().subscribe(response)...
每当输入 this.service
出现自动建议时,在 suggestion 中,建议方法名,一旦接受建议,它只占用不带括号的方法名()。
this.service.method.subscribe()..
=> 会报错
this.service.method().subscibe()..
=> 正确