简体   繁体   English

TypeScript类型化数组声明错误

[英]TypeScript typed array declaration error

For a given class I am declaring a few private variables in the constructor, and making some calls. 对于给定的类,我在构造函数中声明了一些私有变量,并进行了一些调用。 I can getting a No Provider error on my typed array declaration when including it in the constructor, and no errors when declaring it outside the constructor. 将其包含在构造函数中时,在类型化数组声明上会出现No Provider错误,而在构造函数之外进行声明时则不会出错。

This code works, error free 此代码有效,无错误

  private data: Array<MyType>;

  constructor(private service: Service) {
    this.service.getData().then(response => this.data = response.json());
  }

Then, paradoxically, the below does not work, and produces the error found below the snippet. 然后,自相矛盾的是,以下内容不起作用,并在代码段下方产生了错误。

  constructor(private data: Array<MyType>, private service: Service) {
    this.service.getData().then(response => this.data = response.json());
  }

Error from console is 来自控制台的错误是

EXCEPTION: Error: Uncaught (in promise) ORIGINAL EXCEPTION: No provider for Array! 例外:错误:未捕获(承诺)原始例外:没有Array的提供程序!

Is this some syntax error? 这是语法错误吗? Am I horribly misunderstanding a concept? 我是一个可怕的误解吗? Both implementations appear identical and feel like they should behave exactly the same. 两种实现看起来都相同,并且感觉它们应该表现完全相同。

The code is equivalent... except for the call to the constructor. 该代码是等效的...除了对构造函数的调用。 ;) ;)

In the first case: 在第一种情况下:

new TheClass(service);

In the second case: 在第二种情况下:

new TheClass(somethingHere, service);

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

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