【发布时间】:2016-10-02 04:31:05
【问题描述】:
当我尝试使用创建带有参数的类的新实例时出现此错误,但当不使用参数时它工作正常。可能是什么问题?
这是课程
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;
}
}
这是组件
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() {
}
}
如果我分别在 recipe.ts 和 recipe-list.component.ts 中执行此操作:
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() {
}
}
完美运行。
【问题讨论】:
-
不使用
Recipe类为什么要作为provider传呢? -
你不需要在providers数组中注入依赖
-
@yurzui 我还在 8 秒内添加了相同的 cmets:p
-
对angular很陌生,在网上看到了一些解决方案,所以尝试了一下
标签: angular