类型 '() =\> Observable<any>' 上不存在属性 'subscribe'

新手上路,请多包涵

服务文件

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 许可协议

阅读 684
2 个回答

您没有调用 getVideos 方法。您在 subscribe getVideos 而不是返回值。调用 subscribe 后调用 getVideos()

 ngOnInit() {
    this.videoserv.getvideos().subscribe((response) => {
         this.videos = response
    });
}

原文由 Saravana 发布,翻译遵循 CC BY-SA 4.0 许可协议

this.service.method().subscribe(response)...

每当键入 this.service 并出现自动建议时,在建议中,建议方法名称,一旦接受建议,它只占用不带括号()的方法名称。

this.service.method.subscribe().. => 会报错

this.service.method().subscibe().. => 正确

原文由 Nikila VijayaRaghavan 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进