属性'subscribe'在类型上不存在

时间:2017-07-02 07:31:55

标签: angular

我正在构建一个角度4组件并编写一个服务来返回结果集。我在尝试调用movieservice的代码中的电影组件中收到编译错误。错误发生在movie组件的getMovies方法中。不确定是什么问题

错误状态属性'subscribe'在IMovie []类型上不存在。

以下是代码

IMovie界面

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

电影服务

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

电影组件

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

  }

}

2 个答案:

答案 0 :(得分:2)

您的getMovies()未返回Observable类型,因此无法订阅。你需要用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
        }
    ]);
}

答案 1 :(得分:2)

您正在返回IMovie类型的电影列表数组,您需要进行一些修正, 由于您没有返回Observable,但您正在尝试订阅这就是您收到错误的原因。

电影组件

当你收到IMovie类型对象的集合时,你的movieList变量应该是IMovie的数组。

public movieList : IMovie[]

您的服务应该是这样的 -

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

然后你应该从你的getMovies方法返回Observable类型数据。现在你的方法应该是这样的

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

我认为它会正常工作,如果你有任何疑问,你可以问任何问题,Happy Coding