类型 '() => 上不存在属性 'subscribe'Observable<any>' [英] Property &#39;subscribe&#39; does not exist on type &#39;() =&gt; Observable&lt;any&gt;&#39;

查看:30
本文介绍了类型 '() => 上不存在属性 'subscribe'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() 然后它显示该属性的错误类型 ()=> observable

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'Observable<any>'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆