【问题标题】:angular no overload matches this call angular between 2 services 13 typescript角度无重载匹配此调用 2 服务之间的角度 13 打字稿
【发布时间】:2022-01-04 14:25:38
【问题描述】:

我正在按照教程创建一个新的 Angular 项目,但我收到一个我不理解的错误

没有重载匹配这个调用。 重载 1 of 5, '(next: null | undefined, error: (error: any) => void, complete?: (() => void) | undefined): Subscription',给出以下错误。 '(response: PostModel[]) => void' 类型的参数不可分配给 'null |不明确的'。 类型 '(response: PostModel[]) => void' 不可分配给类型 'null'。 重载 2 of 5, '(next?: ((value: Object) => void) | undefined, error?: ((error: any) => void) | undefined, complete?: (() => void) | undefined): Subscription',给出了以下错误。 '(response: PostModel[]) => void' 类型的参数不能分配给'(value: Object) => void' 类型的参数。 参数“响应”和“值”的类型不兼容。 “对象”类型可分配给极少数其他类型。您的意思是改用“任何”类型吗? “Object”类型缺少“PostModel[]”类型的以下属性:length、pop、push、concat 和 26 more.ts(2769)

发布模型

从'./user.model'导入{UserModel};

export class PostModel {
  constructor(
    public id: number,
    public title: string,
    public content: string,
    public image?: string,
    public user?: UserModel,


  public created_at?: string,
  public updated_at?: string,
  ) {}
}

静态服务

import { Injectable } from '@angular/core';
import {HttpHeaders} from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class StaticService {
  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/json',
      // 'Authorization': 'Basic ' + btoa('test:123456')
      
    })
  };
  baseUrl = 'http://localhost:8080/';
  

  constructor() { }
}

邮政服务

import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Subject} from 'rxjs';
import {PostModel} from '../models/post.model';
import {StaticService} from './static.service';

@Injectable({
  providedIn: 'root'
})
  export class PostService {
    private posts = new Subject<PostModel[]>();
    public posts$ = this.posts.asObservable();
  
    constructor(private http: HttpClient, private staticService: StaticService) {}
  
    getPosts() {
      this.http.get(this.staticService.baseUrl + 'posts/all', this.staticService.httpOptions).subscribe(
        (response: PostModel[]) => {
          this.posts.next(response);
        }, (error) => {
          console.log(error);
        }
      );
      return this.posts$;
    }

  getPost(id: number) {
   return this.http.get(this.staticService.baseUrl + 'posts/' + id, this.staticService.httpOptions);
  }

  savePost(post: PostModel) {
   return this.http.post(this.staticService.baseUrl + 'posts', post, this.staticService.httpOptions);
  }

  updatePost(post: PostModel) {
    return this.http.put(this.staticService.baseUrl + 'posts/' + post.id, post, this.staticService.httpOptions);
  }

  deletePost(id: number) {
    return this.http.delete(this.staticService.baseUrl + 'posts/' + id, this.staticService.httpOptions);
  }
}

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    在一个地方缺少类型,并且角度无法匹配未知与 PostModel[]。 在此处修复该添加类型

    getPosts() {
          this.http.get<PostModel[]>(
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-16
      • 2019-08-10
      • 2019-05-09
      • 2023-03-29
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多