首页 > 解决方案 > Angular“错误TS2559:类型'字符串'没有与类型'{ headers?:HttpHeaders”相同的属性

问题描述

我有以下代码试图从 ng 新模板制作一个角度应用程序。我遵循这个作为指导。https://malcoded.com/posts/angular-backend-express

谷歌搜索时,我在 typescript github 上发现这是最接近我的问题的东西。https://github.com/Microsoft/TypeScript/issues/24835

同样在提出这个问题时,我遇到了这个问题,但我认为编写的解决方案不会解决我的问题。错误 TS2559:类型 'Headers' 没有与类型 'RequestOptionsArgs' 相同的属性

app.component.ts

import { Component } from '@angular/core';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
export interface Data {
data: string;
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
  })

@Injectable()
export class AppComponent {
  constructor(private http: HttpClient) {}
  title = 'My Star Database';

  Character(name: string) 
  {
  }
  sendCharacter(name: string): Observable<Data> {
          return this.http.get<Data>('http://localhost:8000/api/character/', name);
                  }
                  getCharacterData(name: string): Observable<Data> {
                          var data =  this.http.get<Data>('http://localhost:8000/api/character/', name);
                          console.log(data);
                          }

  }

错误

ERROR in src/app/app.component.ts(24,71): error TS2559: Type 'string' has no properties in common with type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.
src/app/app.component.ts(26,37): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
src/app/app.component.ts(27,78): error TS2559: Type 'string' has no properties in common with type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.

旁注 另外,我想在代码中添加它,我添加了导出接口并在获取请求中更改为,因为我在具有字符串返回类型的获取请求中收到错误。这是我第一次用打字稿写作的经验,我不得不说那个错误(我已经消失了)和你在上面看到的那个我不觉得打字稿真的配得上它的名字。我希望有角度的开发人员坚持使用 javascript。抱歉,我对这种语言感到非常沮丧,而且我对编程并不陌生。我喜欢三个 C 和 Python。

更新 app.components.ts

import { Component } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
export interface Data {
data: string;
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
  })

export class AppComponent {
  constructor(private http: HttpClient) {}
  title = 'My Star Database';

  Character(name: string) 
  {
  }
  sendCharacter(name: string): Observable<Data> {
          return this.http.get<Data>('http://localhost:8000/api/character/?name='+ name);
                  }
                  getCharacterData(name: string): {
                           data =  this.http.get<Data>('http://localhost:8000/api/character/?name='+ name);
                          console.log(data);
                          }

  }

src/app/app.component.ts(27,7) 中的错误 错误:错误 TS1131:需要属性或签名。src/app/app.component.ts(28,13): 错误 TS1005: ';' 预期的。src/app/app.component.ts(31,3):错误 TS1128:需要声明或声明。

标签: angulartypescript

解决方案


错误 1 ​​是由于在http.get函数中传递了无效参数。http.get的第二个参数应该是标头。如果要在 get 中传递参数,请将其作为 URL 参数包含在内。

return this.http.get<Data>('http://localhost:8000/api/character/?name='+name);

错误 2 是因为您没有从getCharacterData()函数返回任何内容。要么删除返回选项,即

getCharacterData(name: string) {
    var data =  this.http.get<Data>('http://localhost:8000/api/character/?name='+name);
    console.log(data);
}

或者返回数据,

getCharacterData(name: string): Observable<Data> {
    return this.http.get<Data>('http://localhost:8000/api/character/?name='+name);
}

错误 3 与错误 1 ​​重复


推荐阅读