2017-06-16 93 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

非常感謝。 –

相關問題