Property 'subscribe' does not exist on type '() => Observable<any>'

两盒软妹~` 提交于 2019-12-30 01:51:11

问题


service file

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;
         });
    }

}

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


回答1:


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
    });
}



回答2:


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

You should be calling the getvideos() method on the videoserv service. You were missing the (), getvideos is a method not a property.



来源:https://stackoverflow.com/questions/44092034/property-subscribe-does-not-exist-on-type-observableany

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!