【问题标题】:extracting data from responses in anguar从角度响应中提取数据
【发布时间】:2020-08-03 13:25:28
【问题描述】:

我试图从服务器获取项目列表。然后我将分页添加到系统中,服务器的响应会像这样改变,

{
    "items": [
        {
            "book_ref_no": 12,
            "book_name": "ice and fire",
            "author": "jrr martine",
            "category": "HISTORY",
            "availability": false
        },
        {
            "book_ref_no": 14,
            "book_name": "word war 2",
            "author": "Not me",
            "category": "NOVEL",
            "availability": false
        }
    ],
    "meta": {
        "totalItems": 4,
        "itemCount": 2,
        "itemsPerPage": "2",
        "totalPages": 2,
        "currentPage": 1
    },
    "links": {
        "first": "http://localhost:3000/books?limit=2",
        "previous": "",
        "next": "http://localhost:3000/books?page=2&limit=2",
        "last": "http://localhost:3000/books?page=2&limit=2"
    }
}

谁能帮我从这个响应中提取项目、元和链接。以前我使用下面的代码从请求中提取数据。但当时的反应是完全不同的。

getBooks(){
      this.http.get<Book[]>('http://localhost:3000/books')
      .subscribe(books => {
          this.books = books;
          this.booksUpdated.next(books);
        },
        );
      }

之前的回答是这样的

[
 { book 1...},
 { book 2...},
 ...
]

【问题讨论】:

    标签: angular typescript pagination


    【解决方案1】:

    你可以这样试试。 首先创建一个定义响应的接口。

    
    interface Book{
      book_ref_no: number,
      book_name: string,
      author: string,
      category: string, // or Enum
      availability: boolean
    }
    
    interface Meta{
      totalItems: number,
      itemCount: number,
      itemsPerPage: string,
      totalPages: number,
      currentPage: number
    }
    
    interface Links{
      first: string,
      previous: string,
      next: string,
      last: string
    }
    
    interface PagedResponse{
      items: Book[],
      meta: Meta,
      links: Links
    }
    

    然后您可以从该响应中获取书籍,例如,

    getBooks(){
        this.http.get<PagedResponse>('http://localhost:3000/books')
          .subscribe(pagedResponse => {
              this.books = pagedResponse.items;
            },
          );
      }
    

    【讨论】:

    • @Just_Run 很高兴它很有帮助。最好学习一些 Typescript 基础知识;你的问题也与角度无关。因为 typescript 非常关心类型!所以上面它非常关心响应的类型:D
    【解决方案2】:

    首先更新你的界面

    interface Book{ 
    book_ref_no: number, book_name: string, 
    author: string, 
    category: string,
     availability: boolean }
    
     interface Meta{ 
    totalItems: number, 
    itemCount: number,
     itemsPerPage: string, 
    totalPages: number, 
    currentPage: number }
    
     interface Links{ first: string,
     previous: string, 
    next: string, 
    last: string } 
    
    interface BooksResponse{ 
    items: Book[], 
    meta: Meta, 
    links: Links }
    

    然后使用 BooksResponse 作为你的 api 的返回类型

    getBooks(){ 
            this.http.get<BooksResponse>('http://localhost:3000/books')
             .subscribe(data => { 
     this.books = data.items;
        
         this.booksUpdated.next(this.books);
        
             }, ); 
            }
    

    【讨论】:

    • 我需要从响应中提取项目并将它们分配给书籍列表。你能帮我提取它们吗?上面的代码有一些错误。
    • 不应该 get&lt;Book[]&gt; 更改。因为现在的响应不仅仅是一个书单。
    • this.books = data.items;items下方有一条红色下划线
    • 尝试添加任何类型到 data=>.subscribe((data:any) => {
    • 属性'items' 不存在于类型'Book[]'
    猜你喜欢
    • 2021-01-11
    • 2016-02-21
    • 2023-03-04
    • 2022-01-22
    • 2017-05-30
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    相关资源
    最近更新 更多