TypeScript 类型 'IGames[]' 上的错误 TS2322 不可分配
Error TS2322 on TypeScript Type 'IGames[]' is not assignable
我想使用 Http 和 Observable 从 angular 上的 Localhost 获取我的数据,这是我的服务
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { of } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { HttpParams } from '@angular/common/http';
import { IGames } from './games';
@Injectable({
providedIn: 'root'
})
export class GamesService {
constructor(private http: HttpClient) { }
test():Observable<any>{
return of("test");
}
getGames():Observable<IGames[]>{
return this.http.get<IGames[]>('http://localhost/pmn/uas/getgames.php');
}
}
我创建了这个构造函数,所以我可以从任何地方获取它
export interface IGames{id:number, name:string, description:string, pictute:string}
这是我的games.component.ts
import { Component, OnInit } from '@angular/core';
import { GamesService } from '../games.service';
import { IGames } from '../games';
@Component({
selector: 'app-games',
templateUrl: './games.component.html',
styleUrls: ['./games.component.css']
})
export class GamesComponent implements OnInit {
constructor(private gs:GamesService) { }
games=[];
ngOnInit() {
this.gs.getGames().subscribe(
(data)=>{
this.games=data;
console.log=data;
}
);
}
}
此代码虽然有效,但在我的终端上它总是显示此错误
ERROR in src/app/games/games.component.ts(18,9): error TS2322: Type 'IGames[]' is not assignable to type '(message?: any, ...optionalParams: any[]) => void'.
Type 'IGames[]' provides no match for the signature '(message?: any, ...optionalParams: any[]): void'.
对我来说很好,有 2 个变化:1) 在构造函数上方移动 GamesComponent.games
声明,并添加类型:games: IGames[] = [];
2) console.log=data 应该是 console.log(data);
export class GamesComponent implements OnInit {
// members should be above constructor
games: IGames = [];
constructor(private gs:GamesService) { }
ngOnInit() {
this.gs.getGames().subscribe(
(data)=>{
this.games=data;
// console.log is a function
console.log(data);
}
);
}
}
我想使用 Http 和 Observable 从 angular 上的 Localhost 获取我的数据,这是我的服务
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { of } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { HttpParams } from '@angular/common/http';
import { IGames } from './games';
@Injectable({
providedIn: 'root'
})
export class GamesService {
constructor(private http: HttpClient) { }
test():Observable<any>{
return of("test");
}
getGames():Observable<IGames[]>{
return this.http.get<IGames[]>('http://localhost/pmn/uas/getgames.php');
}
}
我创建了这个构造函数,所以我可以从任何地方获取它
export interface IGames{id:number, name:string, description:string, pictute:string}
这是我的games.component.ts
import { Component, OnInit } from '@angular/core';
import { GamesService } from '../games.service';
import { IGames } from '../games';
@Component({
selector: 'app-games',
templateUrl: './games.component.html',
styleUrls: ['./games.component.css']
})
export class GamesComponent implements OnInit {
constructor(private gs:GamesService) { }
games=[];
ngOnInit() {
this.gs.getGames().subscribe(
(data)=>{
this.games=data;
console.log=data;
}
);
}
}
此代码虽然有效,但在我的终端上它总是显示此错误
ERROR in src/app/games/games.component.ts(18,9): error TS2322: Type 'IGames[]' is not assignable to type '(message?: any, ...optionalParams: any[]) => void'.
Type 'IGames[]' provides no match for the signature '(message?: any, ...optionalParams: any[]): void'.
对我来说很好,有 2 个变化:1) 在构造函数上方移动 GamesComponent.games
声明,并添加类型:games: IGames[] = [];
2) console.log=data 应该是 console.log(data);
export class GamesComponent implements OnInit {
// members should be above constructor
games: IGames = [];
constructor(private gs:GamesService) { }
ngOnInit() {
this.gs.getGames().subscribe(
(data)=>{
this.games=data;
// console.log is a function
console.log(data);
}
);
}
}