Angular NGRX从API获取信息

发布于 2025-02-02 03:29:21 字数 622 浏览 5 评论 0原文

我正在尝试将执行OMDB搜索时来自API的信息输入,并将其放在Angular 13中的NGRX范围中。

下面是我试图插入范围的代码,但它指责错误 “类型'电影'丢失了类型'可观察的< movie []>'的属性:源,操作员,提升,订阅等。”

export class MovieSearchComponent implements OnInit {
  
  movies$: Observable<Movie[]>;
  Title = new FormControl;

  constructor(private movieService: MovieService, private store: Store<MovieState>) { 
    this.movies$ = this.store.pipe(select(selectMovies));
  }

  ngOnInit(): void {
  }

  getMovie(): void {

    this.movieService.getMovie(this.Title.value).subscribe(
      movie => {
        this.movies$ = movie
      }
    )
  }

}

I'm trying to put the information that comes from the api when doing an omdb search and put it in the scope of NgRx in angular 13.

below is the code of which I am trying to insert the scope but it accuses the error
"Type 'Movie' is missing the following properties from type 'Observable<Movie[]>': source, operator, lift, subscribe, and 3 more."

export class MovieSearchComponent implements OnInit {
  
  movies$: Observable<Movie[]>;
  Title = new FormControl;

  constructor(private movieService: MovieService, private store: Store<MovieState>) { 
    this.movies$ = this.store.pipe(select(selectMovies));
  }

  ngOnInit(): void {
  }

  getMovie(): void {

    this.movieService.getMovie(this.Title.value).subscribe(
      movie => {
        this.movies$ = movie
      }
    )
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱猫软卧 2025-02-09 03:29:21

这个问题应该在此行中

this.movies$ = movie

在您的观察者功能中,您正在尝试分配“电影”值,即电影类型,因此它们不兼容。

要解决此问题,只需声明电影属性广告在观察者中使用它,而不是电影$ $

public movie: Movie
[...]
this.movie = movie

The issue should be in this line

this.movies$ = movie

the type of this.movies$ is Observable<Movie[]>, an observable that emits an array of Movie values.

In your observer function, your are trying to assign the "movie" value, that is of Movie type, so they are incompatible.

To solve this issue, just declare the movie property ad use it in your observer, instead of movies$

public movie: Movie
[...]
this.movie = movie
错々过的事 2025-02-09 03:29:21

您需要更改以下代码

getMovie(): void {
    this.movies$ = this.movieService.getMovie(this.Title.value)
  }

,并且必须从服务文件返回电影阵列。

You need to change the below code

getMovie(): void {
    this.movies$ = this.movieService.getMovie(this.Title.value)
  }

and you must return Movie array from your service file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文