【问题标题】:Error: Can't resolve all parameters for Classname(? ? ?)错误:无法解析类名的所有参数(???)
【发布时间】: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


【解决方案1】:

问题应该出在this关键字

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);
    }
  ....
}

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 情况下,打字稿在具有参数化构造函数模型时不允许创建对象实例
  • @PankajParkar 你现在可以查看答案了。
  • 请尝试在构造函数之外创建对象的实例,就像 OP 正在做的那样
  • @PankajParkar 您可以查看演示中提供的演示。我已经用我的 VisualCode 和它的工作测试了它。
  • 哦..我看错了一些东西,我无法在手机上查看 plunkr,对此 +1 感到抱歉
猜你喜欢
  • 2019-04-25
  • 2017-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
相关资源
最近更新 更多