Json 角形2/4-Can';t解析GameEditComponent的所有参数:([对象对象],[对象对象],?)
我正在开发应用程序的服务,但当我尝试加载页面时,它显示以下错误: 无法解析GameEditComponent:([对象])的所有参数, [对象],?) 我在服务中尝试将其作为数组放置,或者只保留任何数组,但即使如此,错误仍在继续 game-edit.service.tsJson 角形2/4-Can';t解析GameEditComponent的所有参数:([对象对象],[对象对象],?),json,angular,rest,service,Json,Angular,Rest,Service,我正在开发应用程序的服务,但当我尝试加载页面时,它显示以下错误: 无法解析GameEditComponent:([对象])的所有参数, [对象],?) 我在服务中尝试将其作为数组放置,或者只保留任何数组,但即使如此,错误仍在继续 game-edit.service.ts import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import { Observable } from 'rxjs
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs';
@Injectable()
export class GameEditService {
constructor(private http: Http) { }
getGame(id): Observable<any> {
return this.http.get('http://localhost:8080/lightning/api/game' + id).map(res => res.json()).catch(error => {
throw new Error(error.message);
});
}
getManufactures(): Observable<any> {
return this.http.get('http://localhost:8080/lightning/api/manufacture').map(res => res.json()).catch(error => {
throw new Error(error.message);
});
}
getPlatforms(): Observable<any> {
return this.http.get('http://localhost:8080/lightning/api/platform').map(res => res.json()).catch(error => {
throw new Error(error.message);
});
}
}
这就是即将推出的json,第一个用于选定的游戏,第二个用于平台,第三个用于制造商
选择json游戏
{
"id":1,
"name":"Street Fighter",
"category":"luta",
"price":20.5,
"quantity":1000,
"production":true,
"description":"descricao",
"image":"ps4.jpg",
"manufacture":
{
"id":1,
"name":"Sony",
"image":"ps4.jpg",
"imageFullPath":"http://localhost:8080/lightning/images/ps4.jpg"
}
}
json平台
{
"id":1,
"name":"PC",
"image":"ps4.jpg",
"imageFullPath":"http://localhost:8080/lightning/images/ps4.jpg"
}
json制造商
{
"id":1,
"name":"Sony",
"image":"ps4.jpg",
"imageFullPath":"http://localhost:8080/lightning/images/ps4.jpg"
}
我将angular cli与最新版本中的所有软件包一起使用。
我不知道这个错误是否是因为游戏中的平台,或者其他一些代码问题,如果你知道一些可以修复的东西,我尝试了几种通过互联网找到的解决方案,但都不起作用
提前感谢。问题是组件构造函数中的最后一个参数,
private id
。Angular将尝试解决此依赖关系,但无法为id
找到可注入类。在查看代码时,我认为没有必要将id
注入构造函数。只需将其定义为组件上的属性:
// ... import statements
@Component({
moduleId: module.id,
selector: 'app-game-edit',
templateUrl: './game-edit.component.html',
styleUrls: ['./game-edit.component.css', '../styles.css' ]
})
export class GameEditComponent implements OnInit {
private id; // put the declaration of id here
// remove id declaration from the constructor, no need to inject it
constructor(private activatedRoute: ActivatedRoute,
private gameEditService: GameEditService) { // ...constructor code}
// other code
}
我以其他方式解决了它:我的问题是
HttpClient
有一个罕见的情况,它与app.module上的组件上的“导入”行不同
在组件上是这样的:
import { HttpClient } from '@angular/common/http';
应用程序内模块是:
import { HttpClientModule } from '@angular/common/http';
为什么组件在其构造函数中使用id?非常感谢。
import { HttpClientModule } from '@angular/common/http';