Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 财产';订阅';类型上不存在_Angular - Fatal编程技术网

Angular 财产';订阅';类型上不存在

Angular 财产';订阅';类型上不存在,angular,Angular,我正在构建一个angular 4组件,并编写一个返回结果集的服务。我在试图调用movieservice的代码中的movie组件中遇到编译错误。错误在电影组件中的getMovies方法中。不知道是什么问题 错误状态属性“subscribe”在IMovie[]类型上不存在 下面是代码 IMovie界面 export interface IMovie{ movieId:number; name:string; actor:string; director:string;

我正在构建一个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 
         }
        ];
    }
}
电影服务

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

  }

}
您的
getMovies()
没有返回
可观察的类型,因此无法订阅。您需要用
Observable.of()将其包装起来。

getMovies():可观察{
可观测收益([
{
影片编号:1,,
名称:“泰坦尼克号”,
演员:“测试1”,
导演:“测试2”,
电影长度:2
},
{
影片编号:2,,
名称:“泰坦尼克号”,
演员:“测试1”,
导演:“测试2”,
电影长度:2
},
{
影片编号:3,,
名称:“泰坦尼克号”,
演员:“测试1”,
导演:“测试2”,
电影长度:2
}
]);
}
您的
getMovies()
没有返回可观察的
类型,因此无法订阅。您需要用
Observable.of()将其包装起来。

getMovies():可观察{
可观测收益([
{
影片编号:1,,
名称:“泰坦尼克号”,
演员:“测试1”,
导演:“测试2”,
电影长度:2
},
{
影片编号:2,,
名称:“泰坦尼克号”,
演员:“测试1”,
导演:“测试2”,
电影长度:2
},
{
影片编号:3,,
名称:“泰坦尼克号”,
演员:“测试1”,
导演:“测试2”,
电影长度:2
}
]);
}

您正在返回IMovie类型的电影列表数组,需要进行一些更正, 因为您没有返回可观察的,但您正在尝试订阅,这就是为什么会出现错误。

电影组件

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

  }

}
由于您正在接收IMovie类型对象的集合,因此您的movieList变量应该是IMovie的数组。 你的服务应该是这样的- 然后,您应该从getMovies方法返回可观察的类型数据。现在,您的方法应该如下所示

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
        }
    ]);
    }
getMovies():可观察{
可观测收益([
{
影片编号:1,,
名称:“泰坦尼克号”,
演员:“测试1”,
导演:“测试2”,
电影长度:2
},
{
影片编号:2,,
名称:“泰坦尼克号”,
演员:“测试1”,
导演:“测试2”,
电影长度:2
},
{
影片编号:3,,
名称:“泰坦尼克号”,
演员:“测试1”,
导演:“测试2”,
电影长度:2
}
]);
}

我认为它可以正常工作,如果你有任何疑问,你可以问任何问题,快乐编码你正在返回IMovie类型的电影列表数组,需要一些更正, 因为您没有返回可观察的,但您正在尝试订阅,这就是为什么会出现错误。

电影组件

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

  }

}
由于您正在接收IMovie类型对象的集合,因此您的movieList变量应该是IMovie的数组。 你的服务应该是这样的- 然后,您应该从getMovies方法返回可观察的类型数据。现在,您的方法应该如下所示

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
        }
    ]);
    }
getMovies():可观察{
可观测收益([
{
影片编号:1,,
名称:“泰坦尼克号”,
演员:“测试1”,
导演:“测试2”,
电影长度:2
},
{
影片编号:2,,
名称:“泰坦尼克号”,
演员:“测试1”,
导演:“测试2”,
电影长度:2
},
{
影片编号:3,,
名称:“泰坦尼克号”,
演员:“测试1”,
导演:“测试2”,
电影长度:2
}
]);
}

我认为它可以很好地工作,如果你有任何疑问,你可以问任何问题,很高兴知道你为什么
。订阅
?该方法只返回一个数组,而不是一个可观察的数组,正如您键入并编写的那样。那么,您为什么
。subscribe
?该方法只返回一个数组,而不是一个可观察的数组,正如您键入并编写的那样。@Aravind当我显式地编写代码,他应该使用
observable.of()
返回一个可观察的数组时,您的这个副本怎么样了。但是如果downvote让你感觉更好,那就继续吧。@Aravind当我明确地写下他应该使用
Observable.of()
返回一个Observable的代码时,你的这个副本怎么样了。但如果否决票让你感觉更好,那就继续吧。