角5-可观察到的返回错误无法读取未定义的属性

狂犬病

当我使用httpClient调用我的Web服务时,我开始在我的角度服务中使用Spring Rest和Angular 5实现简单的分页,并获得正确的请求页面响应,它在页面上正确显示数据,但控制台网络出现错误浏览器在我的* ngFor循环中存在错误,尽管模板显示正确,但无法读取undefined属性:

问题出在Service方法中:getPageClient()和* ngFor指令这是我的错误日志: 在此处输入图片说明

这是我使用可观察的服务:

import { Injectable } from '@angular/core';
import {Client} from '../models/Client'
import {PageClient} from '../models/PageClient'
import { Observable, of } from 'rxjs';
import { HttpClient,HttpHeaders } from '@angular/common/http';
import {catchError,map,tap} from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class ClientService {

  private url = 'http://localhost:8099/api/clients';

  private urlPage = 'http://localhost:8099/api/clients/get?page=0&size=3';

  getClient(): Observable<Client[]>{
     return this.http.get<Client[]>(this.url)
      .pipe(
           catchError(this.handleError('getClient', []))
      );
  }

 getPageClient(): Observable<PageClient>{
  return this.http.get<PageClient>(this.urlPage)
  .pipe(
    map(response => {
      const data = response;
      console.log(data.content);
      return data ;
    }));
}
  constructor(private http : HttpClient) { }

  private handleError<T> (operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {

      // TODO: send the error to remote logging infrastructure
      console.error(error); // log to console instead

      // TODO: better job of transforming error for user consumption
      console.log(`${operation} failed: ${error.message}`);

      // Let the app keep running by returning an empty result.
      return of(result as T);
    };
  }
}

这是我的模特班:

import {Client} from '../models/Client' ;  

export class PageClient {
    content : Client[];
    totalPages : number;
    totalElements : number;
    last : boolean;
    size : number ;
    first : boolean ;
    sort : string ;
    numberOfElements : number ;
}

这是我的组件代码:

import { Component, OnInit } from '@angular/core';
import {Client} from '../models/Client' ;
import {PageClient} from '../models/PageClient'
import {ClientService} from '../services/client.service' ;

@Component({
  selector: 'app-clients',
  templateUrl: './clients.component.html',
  styleUrls: ['./clients.component.css']
})
export class ClientsComponent implements OnInit {

  clients : Client[];
  pageClient : PageClient ;

  getClient(): void {
    this.clientService.getClient()
        .subscribe(clients => this.clients = clients);
  }

  getPageClient(): void {
    this.clientService.getPageClient()
        .subscribe(page => this.pageClient = page);

  }
  constructor(private clientService : ClientService) { }

  ngOnInit() {
     this.getClient();
     this.getPageClient();
  }

}

这是我实现分页的模板部分:

<nav aria-label="...">
  <ul class="pagination"  *ngIf="pageClient.content" >
    <li class="page-item disabled">
      <a class="page-link" href="#" tabindex="-1">Previous</a>
    </li>
    <li *ngFor="let page of pageClient.content ; let i=index " class="page-item"><a class="page-link" href="#">{{i}}</a></li>
   <!-- <li class="page-item active">
      <a class="page-link" href="#">2 <span class="sr-only">(current)</span></a>
    </li>-->
    <li class="page-item">
      <a class="page-link" href="#">Next</a>
    </li>
  </ul>
</nav>

有人可以提前帮我找到问题吗?

用户名

由于该getPageClient函数是异步的,pageClient因此在首次加载页面时未定义。这意味着这样做时pageClient.content,您会得到一个错误。

幸运的是,Angular提供了一些有用的语法,您可以在模板中使用以避免这种情况。您应该*ngIf="pageClient?.content" >改用。

?告诉角度为只读content,如果pageClient不为空/未定义。

更多信息在这里

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

角“无法读取未定义的属性'subscribe'”

可观察到的类型错误:无法读取未定义的属性

角2:无法读取未定义的属性“验证器”

Angular2可观察到的问题,无法读取未定义的属性“订阅”

Angular 5-“无法读取未定义的属性”

TypeError:无法读取未定义的属性'splice'-Angular 5

角反应形式错误| 错误TypeError:无法读取未定义的属性“有效”

角4:无法读取未定义的属性“ http”

Angular 5无法读取未定义的属性“删除”

角6:TypeError:无法读取未定义的属性“无效”?

Angular2-单元测试可观察到的错误“无法读取未定义的属性'subscribe'”

Angular 5 rxjs 5.5.2-导入间隔错误-“无法读取未定义的属性'调用'

Angular 5 TypeError:无法读取未定义的属性

类型错误:无法读取未定义的属性“ 0” 角4

错误TypeError:无法读取未定义的属性“ html”(角字体)

角5:错误TypeError:无法读取未定义的属性'toLowerCase'

角数组:错误类型错误:无法读取未定义的属性

错误:无法读取未定义Angular 5的属性'cloneDeep'

无法读取未定义的属性“ healthCarePlans”(Angular5)

角“ ERROR TypeError:无法读取未定义的属性”匹配“

角组件测试用例出现错误:类型错误:无法读取未定义的属性“ contractno”

角:从forkJoin返回的值在其他可观察到的管道内

收到错误“ TypeError:无法观察到未定义”

HTTP可观察到的返回未定义/空

Angular 5 无法读取未定义的属性“目标”

角 | 无法读取未定义的属性“XXX”

CKEditor5:无法读取未定义的属性“pluginName”

HTML5 视频:未捕获的类型错误:无法读取未定义的属性“0”

类型错误:无法读取未定义的属性“名称”。角 7