简体   繁体   English

Angular2类构造函数错误:没有Number的提供程序

[英]Angular2 class constructor error: no provider for Number

I've started a project using the development kit found here: https://github.com/datatypevoid/ng2-mean-webpack . 我使用这里的开发工具包开始了一个项目: https//github.com/datatypevoid/ng2-mean-webpack Everything seems to work perfectly fine, except when I create a new class with parameters properties (as seen on the official Typescript documentation here: https://www.typescriptlang.org/docs/handbook/classes.html ), I get an error message in the console saying: 一切似乎工作得很好,除非我创建一个带有参数属性的新类(如官方的Typescript文档中所示: https ://www.typescriptlang.org/docs/handbook/classes.html),我收到错误控制台中的消息说:

ORIGINAL EXCEPTION: No provider for Number!

Here is my class: 这是我的班级:

@Component({
  selector: 'grid-tile',
  template: require('./grid-tile.component.html'),
  styleUrls: [require('!style!css!sass!./grid-tile.component.scss')],
  providers: [],
  directives: [],
  pipes: []
})
export class GridTile extends Tile {
  constructor(private x:number, private y: number) {
    super();
  }
}

And a here is how I create the instances: 以下是我创建实例的方法:

var tile = new GridTile(3,4);
tiles.push(tile);

I would really appreciate finding a solution for this problem as I go further into development. 我非常希望在进一步开发的过程中找到解决这个问题的方法。 I've followed the Angular2 Tour of Heroes tutorial and parameter properties seemed to work fine, so I'm wondering if it has something to do with the development kit or simply something I'm passing on.. I'm totally new with Angular2, so this may be a mistake by my fault. 我已经按照Angular2 Tour of Heroes教程和参数属性似乎工作得很好,所以我想知道它是否与开发工具包有关或者只是我正在传递的东西..我对Angular2完全是新的所以这可能是我的错误。 Thanks for the help. 谢谢您的帮助。

Angular 2 uses MVC pattern: a class decorated with @Component is equivalent to a Controller. Angular 2使用MVC模式:用@Component修饰的@Component等同于Controller。 This means that: 这意味着:

  • you can not provide parameters to the constructor if you do not use Dependency Injection (this generates the error); 如果不使用依赖注入(这会产生错误),则无法为构造函数提供参数;

  • the class should not be instantiated, but must be invoked at the time of the bootstrap, or through a router, or in Angular passing it as a child through a directive. 该类不应该被实例化,但必须在引导时或通过路由器调用,或者在Angular中通过指令将其作为子项传递。

If you carefully analyze the Tour of Heroes , you will notice that you create a service, that is an injectable class: 如果你仔细分析英雄 ,你会发现你创建了一个服务,这是一个注射类:

@Injectable()
export class HeroService {
  ...
  }
}

Then you inject it as a provider (in the same component or in a parent), and instantiate by passing it to the constructor of the component: 然后将其作为provider (在同一组件或父级中)注入,并通过将其传递给组件的构造函数来实例化:

@Component({
  selector: 'my-app',
  ...
  providers: [HeroService]
})
export class AppComponent implements OnInit {
  ...
  constructor(private heroService: HeroService) { }
  ...
}

In this way the reference to the injected class is inherited by the component and by all child components. 通过这种方式,对注入类的引用由组件和所有子组件继承。

In your example, you have two variables as parameters, and the error is generated because they don't correspond to any provider in the component. 在您的示例中,您有两个变量作为参数,并且生成错误,因为它们不对应于组件中的任何提供程序。

You can read more here . 你可以在这里阅读更多。

Or, to begin: http://victorsavkin.com/post/118372404541/the-core-concepts-of-angular-2 或者,开始: http//victorsavkin.com/post/118372404541/the-core-concepts-of-angular-2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM