2 回答
TA贡献1802条经验 获得超5个赞
从您的 http 调用中删除对象。使用泛型是可选的,尤其是在您没有输入回复的情况下。
import { Injectable } from '@angular/core';
import {HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class HttpService {
constructor(private http: HttpClient) { }
myMethod() {
return this.http.get('https://localhost:44398/api/boggle');
}
myWordMethod(word) {
var url = 'https://localhost:44398/api/wordpoints/' + word;
return this.http.get(url);
}
}
在 brews 上,将其声明为 any:
import { Component, OnInit } from '@angular/core';
import { HttpService } from '../http.service';
@Component({
selector: 'app-playboard',
templateUrl: './playboard.component.html',
styleUrls: ['./playboard.component.scss']
})
export class PlayboardComponent implements OnInit {
brews: any;
constructor(private _http: HttpService) { }
ngOnInit() {
this._http.myMethod().subscribe(data => {
this.brews = data;
this.dices = this.brews.myBox;
this.diceSeed = this.brews.boxID;
console.log(this.brews);
});
}
TA贡献1853条经验 获得超9个赞
您可以通过简单地将它们定义为 any 类型来忽略它们。例如;
myWordMethod(word: any) {
..
}
this._http.myMethod().subscribe(data: any => {
..
});
也就是说,通常首选为 TypeScript 声明实际类型。例如,如果您的 API 发回具有特定属性的通用对象,则将其声明为这样;
interface MyMethodResponse {
someProperty: string;
someNumber: number;
someArray: string[];
}
this._http.myMethod().subscribe((myMethodResponse: MyMethodResponse) => {
// TypeScript now knows that these properties exists on the response object
console.log(myMethodResponse.someArray);
console.log(myMethodResponse.someNumber);
console.log(myMethodResponse.someProperty);
});
添加回答
举报