2016-10-01 7 views
2

パラメータを使用してクラスの新規インスタンスを作成しようとするとこのエラーが発生しますが、パラメータを使用しない場合はうまく動作します。何が問題なの?ここでエラー:クラス名(??)のすべてのパラメータを解決できません

はここでクラス

export class Recipe { 
    public name: string; 
    public description: string; 
    public imageP: string; 

    constructor(name1: string, description1: string, imagePath1: string) { 
    this.name = name1; 
    this.description = description1; 
    this.imageP = imagePath1; 
    } 

} 

である代わりに、私はそれぞれrecipe.tsレシピ-list.component.tsでこれを行う場合、コンポーネント

import {Component, OnInit, Output} from '@angular/core'; 
import { Recipe } from '../recipe'; 

@Component({ 
    selector: 'rb-recipes-list', 
    templateUrl: './recipes-list.component.html', 
    providers: [ Recipe ] 
}) 
export class RecipesListComponent implements OnInit { 

    theRecipe: Recipe = new Recipe('New', 'blah blah blah', 'ddkkkiskxmdks'); 

    recipe = this.theRecipe.name; 

    constructor() { } 

    ngOnInit() { 
    } 

} 

です:

export class Recipe { 
    public name: string = 'hello world'; 
    public description: string = 'hello world'; 
    public imageP: string = 'hello world'; 

} 


import {Component, OnInit, Output} from '@angular/core'; 
import { Recipe } from '../recipe'; 

@Component({ 
    selector: 'rb-recipes-list', 
    templateUrl: './recipes-list.component.html', 
    providers: [ Recipe ] 
}) 
export class RecipesListComponent implements OnInit { 
    //recipes: Recipe[] = []; 

    theRecipe: Recipe = new Recipe() 

    recipe = this.theRecipe.name; 

    constructor() { } 

    ngOnInit() { 
    } 

} 

は完全に機能します。

+1

なぜあなたはそれを使用しない場合、あなたは 'Recipe'クラスをプロバイダとして渡しますか? – yurzui

+0

プロバイダに依存関係を注入する必要はありません。配列 –

+0

@yurzui 8秒の違いで同じコメントを追加しました:p –

答えて

3

問題がこのキーワード

export class RecipesListComponent implements OnInit { 

    theRecipe: Recipe = new Recipe('New', 'blah blah blah', 'ddkkkiskxmdks'); 

    // recipe = this.theRecipe.name;   //<<<===removed this line 

    recipe = theRecipe.name;     //<<===removed this keyword 

} 

UPDATEにする必要があります:私はちょうどこれをテストし、完璧に動作します。

export class RecipesListComponent implements OnInit { 

    theRecipe:Recipe; 

    constructor(){ 
     this.theRecipe = new Recipe('New', 'blah blah blah', 'ddkkkiskxmdks'); 
     console.log(this.theRecipe.name); 
    } 
    .... 
} 

OR

export class RecipesListComponent implements OnInit { 

     theRecipe:Recipe; 
     theRecipe = new Recipe('New', 'blah blah blah', 'ddkkkiskxmdks'); 
     constructor(){ 
      console.log(this.theRecipe.name); 
     } 
     .... 
} 

NOTEproviders:[Recipe]を削除します(その必要はありません)。 をインポートするだけで、すでに使用している場所であればいつでも使用できます。

アップデート1:私は、あまりにもplunkerとその作業でそれをテストしている

DEMO:https://plnkr.co/edit/Vxdeyjwdjol9TnVhCV19?p=previewチェックブラウザのコンソール。

+1

あなたは正しい問題を指摘したと思います。それは別です。 OPケースでは、何らかの形で、オブジェクトのインスタンスを作成することができません。コンストラクタモデルをパラメータ化しています。 –

+1

@PankajParkarここで答えを確認できます。 – micronyks

+0

OPの処理のように、コンストラクタのすぐ外側にオブジェクトのインスタンスを作成しようとしますか? –

関連する問題