Search code examples
angularobjectobservablesubscribe

TS7053: Element implicitly has an 'any' type because expression of type '"page"' can't be used to index type 'Object'


I cannot retrieve the content of the object when I subscribe with observable. I would like to retrieve the number of totalPages.

This is my code:

  getProducts() {
    this.catServ.getProductByKeyWord(this.currentkeyword,this.currentPage, this.size)
      .subscribe(data=>{

        this.totalPages=data["page"].totalPages;

      },err=>{
        console.log(err);
      })  }

This is te structure of the object :

_embedded   {…}
_links  {…}
page:
    size    :2
    totalElements   :56
    totalPages  :28
    number  :0

The solution data.page.totalPages; does not work.


Solution

  • Try this:

    this.totalPages=(data as any).page.totalPages
    

    Thanks very much, your solution work and this also work

    this.produits=data;
    this.totalPages=this.produits.page.totalPages;