简体   繁体   English

属性“订阅”在类型上不存在

[英]Property 'subscribe' does not exist on type

I am building an angular 4 component and writing a service to return a resultset. 我正在构建一个angular 4组件并编写服务以返回结果集。 I am getting a compile error in the movie component in the code where its trying to call the movieservice. 我在尝试调用movieservice的代码中的movie组件中遇到编译错误。 The error is in the getMovies method in movie component. 错误是在电影组件的getMovies方法中。 Not sure what the problem is 不确定是什么问题

The error states Property 'subscribe' does not exist on type IMovie[]. 错误状态IMovie []类型上不存在属性'subscribe'。

Below is the code 下面是代码

IMovie interface IMovie界面

export interface IMovie{
    movieId:number;
    name:string;
    actor:string;
    director:string;
    movieLength:number;  
}

Movie service 电影服务

import {Injectable} from '@angular/core';
import {MRDBCommonService} from '../shared/services/mrdb.common.service';
import {IMovie} from './movie.interface';
const URL_MOVIE = '/api/movie';

@Injectable()
export class MovieService 
{

constructor(private _mrdbCommonService: MRDBCommonService){}

    getMovies() : IMovie[] {
        return[
         {
            movieId:1,
            name:"Titanic",
            actor:"Test1",
            director:"Test2",
            movieLength : 2 
         },
         {
            movieId:2,
            name:"Titanic",
            actor:"Test1",
            director:"Test2",
            movieLength:2 
         },
         {
            movieId:3,
            name:"Titanic",
            actor:"Test1",
            director:"Test2",
            movieLength: 2 
         }
        ];
    }
}

Movie component 电影组件

import {Component, OnInit} from '@angular/core';
import {MovieService} from './movie.service';
import {IMovie} from './movie.interface';


@Component({
  moduleId: module.id,
  selector: 'app-movie',
  templateUrl: './movie.component.html',
  styleUrls: ['./movie.component.css'],
  providers:[MovieService]
})

export class MovieComponent implements OnInit {

public movieList : IMovie = null;

  constructor(private movieSerice : MovieService) {
    this.getMovies();
   }

  ngOnInit() {

  }

  private getMovies()
  {
    this.movieSerice.getMovies().subscribe((result : IMovie) => {

    this.movieList = result;
    });

  }

}

Your getMovies() does not return an Observable type, hence can't be subscribed. 您的getMovies()不返回Observable类型,因此无法订阅。 You need to wrap it with Observable.of() : 您需要使用Observable.of()包装它:

getMovies(): Observable<IMovie[]> {
    return Observable.of([
        {
            movieId: 1,
            name: "Titanic",
            actor: "Test1",
            director: "Test2",
            movieLength: 2
        },
        {
            movieId: 2,
            name: "Titanic",
            actor: "Test1",
            director: "Test2",
            movieLength: 2
        },
        {
            movieId: 3,
            name: "Titanic",
            actor: "Test1",
            director: "Test2",
            movieLength: 2
        }
    ]);
}

You are returning the array of movie list of type IMovie, some correction you need, As you are not returning Observable but you are trying to subscribe that's why you are getting error . 您正在返回类型为IMovie的电影列表的数组,需要进行一些纠正, 因为您没有返回Observable,但是您尝试订阅,这就是您得到error的原因。

Movie component 电影组件

as you are receiving the collection of IMovie type object so your movieList variable should be the array of IMovie. 当您收到IMovie类型对象的集合时,您的movieList变量应为IMovie的数组。

public movieList : IMovie[]

Your service should like this - 您的服务应该像这样-

import {Observable} from "rxjs";// First you need to import Observable

Then you should return Observable type data from your getMovies method. 然后,您应该从getMovies方法返回Observable类型数据。 Now your method should look like this 现在您的方法应如下所示

getMovies(): Observable<IMovie[]> {

        return Observable.of([
        {
            movieId: 1,
            name: "Titanic",
            actor: "Test1",
            director: "Test2",
            movieLength: 2
        },
        {
            movieId: 2,
            name: "Titanic",
            actor: "Test1",
            director: "Test2",
            movieLength: 2
        },
        {
            movieId: 3,
            name: "Titanic",
            actor: "Test1",
            director: "Test2",
            movieLength: 2
        }
    ]);
    }

I think it will works fine and u can ask anything if you have any query, Happy Coding 我认为它将很好用,如果您有任何疑问,您可以问任何问题,快乐编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM