繁体   English   中英

Angular4打字稿; 属性“ data”在类型“ any []”上不存在

[英]Angular4 Typescript; Property 'data' does not exist on type 'any[]'

我正在使用TypeScript在Angular 4上构建应用程序。 我不知道为什么我得到错误

Property 'data' does not exist on type 'any[]'

对于这样的表达。 我已经从组件中删除了这个;

LoadAllUsers(): void {
    this.AllUsers.Loading = true;
    let AdditionalParams = {
      'page': this.AllUsers.CurrentPage
    };
    this.UserService.All(AdditionalParams)
        .subscribe(
            (users) => {
              this.AllUsers.Users = users.data;
              this.AllUsers.Data = users;
              this.AllUsers.Loading = false;
              this.AllUsers.Loaded = true;
              console.log ("Users response: ", this.AllUsers, this.AllUsers.Users);
            },
            (error)=>  {
              this.AllUsers.Loading = false;
              console.log ("THE ERROR: ", error);
              this.AllUsers.RequestStatus = error.status=0;
              if (typeof error.json == 'function' && error.status!=0) {
                let errorObject = error.json();
                this.AllUsers.RequestError = errorObject;
              } else {
                this.AllUsers.RequestError = "net::ERR_CONNECTION_REFUSED";
              }
              this.AllUsers.HasRequestError = true;
            });
  }

我只能使用来绕过此错误; (users) => this.PrepareUsers(users)

我一定缺少有关TypeScript的东西。 奇怪的是我可以使用error.status

我猜您应该将服务中的方法签名更改为All(AdditionalParams: any): Observable<User[]> ,当然,这就是如果您有User模型。 同样,变量和方法应以小写字母开头。

服务(users)参数中返回的数据是一个数组。 所有数组都具有相同的属性和方法。 .data不是数组的属性或方法。

您可以在此处找到Array类型的所有属性和方法

可以帮助您调试特定问题的方法是将(users)登录到控制台,然后查看数据是什么样的:

LoadAllUsers(): void {
    this.UserService.All(AdditionalParams)
        .subscribe(
            (users) => {
              console.log(users);
            });
  }

您的数据可能不会以您期望的形式返回给您。

暂无
暂无

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

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