“void”不能赋给类型为“”的参数(值:Object)=>void”,以角形、 typescript 格式

5ssjco0h  于 2023-02-13  发布在  TypeScript
关注(0)|答案(2)|浏览(463)

我不能解决这个问题。我在. net中开发了web api,我正在尝试调用angular上的api。

//movie.ts 
export class Movie{
    ID: number;
    Title: number;
    GenreId: number;
    Duration: number;
    RatingScore: number;
    PublishDate: Date;
}





//movieservice.ts
import { Injectable, Inject } from "@angular/core";
import {HttpClient} from "@angular/common/http"
import {} from "@angular/core/"
import { Movie } from "../models/movie";

@Injectable({
  providedIn: 'root'
})
export class MovieserviceeService {

  constructor(private httpClient: HttpClient, @Inject("url") private url: string) { }

    GetAll() {
        return this.httpClient.get(this.url + "Movies");
      }
      GetSingle(id: number) {
        return this.httpClient.get(this.url + "Movies/" + id);
      }
      PostAdd(personel: Movie) {
        return this.httpClient.post(this.url + "Movies", personel);
      }
      PutUpdate(personel: Movie) {
        return this.httpClient.put(this.url + "Movies/", personel);
      }
      Remove(id: number) {
        return this.httpClient.delete(this.url + "Movies?id=" + id);
      }
}





//appmovie.component.ts
import { Component, OnInit } from '@angular/core';
import {Movie} from "../../models/movie"
import { MovieserviceeService } from 'src/app/services/movieservicee.service';
;
@Component({
  selector: 'app-appmovie',
  templateUrl: './appmovie.component.html',
  styleUrls: ['./appmovie.component.css']
})
export class AppmovieComponent implements OnInit {
  movieList: Movie[];
  movie: Movie;
  constructor(private MovieserviceeService: MovieserviceeService){}
  ngOnInit(): void {
    throw new Error('Method not implemented.');
  }

  GetAll()
  {
    this.MovieserviceeService.GetAll().subscribe((response: Movie[]) => {this.movieList = response; });
  }

}

我得到这个错误,
没有与此调用匹配的重载。重载1/3,'(观察者?: Partial):Subscription ",出现以下错误。键入"(响应:Movie [])=〉void "与类型" Partial "没有共同的属性。重载3个中的第2个,"(下一个:(值:对象)=〉无效):订阅"",出现以下错误。类型""的参数(响应:Movie [])=〉void "不能赋给类型为""的参数(值:Object)=〉void "。参数" response "和" value "的类型不兼容。" Object "类型只能分配给少数其他类型。是否要改用" any "类型?类型" Object "缺少类型" Movie []"的以下属性:长度,弹出,推动,concat,和29个以上。超载3的3,'(下一个?:(值:对象)=〉无效,错误?:(错误:任何)=〉无效,是否完成?:()=〉无效):订阅"",出现以下错误。类型""的参数(响应:Movie [])=〉void "不能赋给类型为""的参数(值:Object)=〉void "。参数" response "和" value "的类型不兼容。类型" Object "无法分配给类型" Movie []"。" Object "类型只能分配给少数其他类型。是否要改用" any "类型?

Movie[]) => void' is not assignable to parameter of type '(value: Object) => void'. Types of parameters 'response' and 'value' are incompatible. Type 'Object' is not assignable to type 'Movie[]'. The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?

uubf1zoe

uubf1zoe1#

在服务中使用泛型:

GetAll() {
        return this.httpClient.get<Movie[]>(this.url + "Movies");
      }
csbfibhn

csbfibhn2#

当您尝试将具有不兼容类型的函数传递给Angular 可观测对象时,会出现此错误消息。
若要解决此错误,应检查正在传递的函数是否与Observable的参数的预期类型匹配。在本例中,Observable似乎预期函数将Object作为参数,但您正在传递的函数将接受Movie对象数组。
若要修复此问题,可以将函数参数的类型更改为Object,或者可以将Observable的类型更改为预期的Movie对象数组,而不是Object。

相关问题