【问题标题】:What is the error in this code ? I had add a new class and imported into .ts file , then defined an array这段代码有什么错误?我添加了一个新类并导入到 .ts 文件中,然后定义了一个数组
【发布时间】:2019-04-25 21:30:56
【问题描述】:

控制台显示错误并显示配方未定义。

最后我添加了单独文件的类定义。

导入声明:

import { Recipe } from '../recipe.model';

新配方

export class RecipeListComponent implements OnInit {
    recipes : Recipe[] = [
        new Recipe('Test 1','Test Description','https://www.gimmesomeoven.com/wp-content/uploads/2014/03/Cajun-Jambalaya-Recipe-with-Andouille-Sausage-Shrimp-and-Chicken-3-1.jpg') 
];

    constructor(){}
    ngOnInit() {

    }
    }

类定义:

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

    constructor(name :string, description : string, imagePath : string){
        this.name=name;
        this.description=description;
        this.imagePath=imagePath;
    }
}

【问题讨论】:

  • 课程论坛是问这个问题的更好地方;)查看模块和应用程序文件,看看您是否在所有需要的地方都定义了食谱。

标签: javascript angular typescript


【解决方案1】:

可能是你的声明:

import { Recipe } from '../recipe.model';

 export class RecipeListComponent implements OnInit {
    public recipes: Recipe;
 constructor(){
   this.recipes = new Recipe('Test 1','Test 
   Description','https://www.gimmesomeoven.com/wp-
   content/uploads/2014/03/Cajun-Jambalaya-Recipe-with-Andouille-Sausage-
  Shrimp-and-Chicken-3-1.jpg');
 }

 ngOnInit() {

  }
 }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-12
  • 2019-06-24
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
相关资源
最近更新 更多