[英]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 电影组件
public movieList : IMovie[]
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
}
]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.