2017-06-16 66 views
1

我正在开发我的应用程序的服务,但是当我尝试加载页面时,它显示以下错误:Angular 2/4 - 无法解析GameEditComponent的所有参数:([object Object],[object Object],?)

Can't resolve all parameters for GameEditComponent: ([object Object], [object Object], ?).

我在服务试图把数组或只留下任何,但即使这样的错误继续

游戏edit.service.ts

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); 

     }); 

    } 

} 

游戏-edit.component.ts

import { ActivatedRoute, Params } from '@angular/router'; 
import { Component, OnInit } from '@angular/core'; 
import { GameEditService } from './game-edit.service'; 

@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 { 

    constructor(private activatedRoute: ActivatedRoute, private gameEditService: GameEditService, private id) { 

     this.gameEditService.getPlatforms().subscribe(platforms => { 

      console.log(platforms); 

     }), erro => console.log(erro); 

     this.gameEditService.getManufactures().subscribe(manufactures => { 

      console.log(manufactures); 

     }), erro => console.log(erro); 

    } 

    ngOnInit() { 

     this.activatedRoute.params.subscribe((params: Params) => { 

      this.id = params['id']; 

      console.log(this.id); 

     }); 

     this.gameEditService.getGame(this.id).subscribe(game => { 

      console.log(game); 

     }), erro => console.log(erro); 

    } 

    onSubmit(form){ 

     console.log(form); 

    } 

    verificaValidTouched(campo){ 

     return !campo.valid && campo.touched; 

    } 

    aplicaCssErro(campo){ 

     return { 

      'subError': this.verificaValidTouched(campo) 

     } 

    } 

} 

这是未来的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" 
} 

Console

我使用的是与最新版本的所有包角CLI。 我不知道如果这个错误可能是因为游戏内部的平台或其他代码问题,如果你知道可以修复的东西,我尝试了几种通过互联网找到的解决方案,但都没有成功。

在此先感谢。

+0

为什么组件在其构造函数中使用了一个id? – yadejo

回答

2

问题是组件的构造函数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 
} 
+0

非常感谢。 –

相关问题