错误:无法解析Classname的所有参数(???)
当我尝试使用具有参数的Class创建新实例时,出现此错误,但未使用参数时工作正常。可能是什么问题?错误:无法解析Classname的所有参数(???)
这里是类
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和配方,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() {
}
}
完美地工作。
的问题应该与这关键字
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
}
更新:我只是测试这一点,完美的作品。
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);
}
....
}
注意。删除providers:[Recipe]
(不需要那个)。你只需要进口它无论你想使用它,你已经在做。
UPDATE1:我曾与plunker也和它的工作进行了测试,
DEMO:https://plnkr.co/edit/Vxdeyjwdjol9TnVhCV19?p=preview检查浏览器的控制台。
我想你指出了一个正确的问题,但那是另一回事。在OP案例中,typecript不允许在参数化构造器模型时创建对象的实例。 –
@PankajParkar现在可以检查答案。 – micronyks
是否尝试在构造函数的外部创建对象的实例,比如OP正在做什么 –
为什么如果你不使用'Recipe'类作为提供者? – yurzui
你不需要注入依赖于提供者数组 –
@yurzui我也在8秒的差异中添加了相同的注释:p –