解开承诺,以获取内部数据

发布于 2025-02-07 18:23:10 字数 1457 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘亓 2025-02-14 18:23:10

您的服务不会返回productTableItem,而是服务器响应。创建两个模型:

export class ProductData {
  products?: ProductTableItem[]
}

现在

export class ServerResponse<T> {
  timestamp: any;
  status: string;
  data: T;
}

在您的服务中:

getProducts(): Observable<ServerResponse<ProductData>> {
  return this.http.get<ServerResponse<ProductData>>(this.productUrl)
    .pipe(
      tap(data => console.log('All', JSON.stringify(data))),
      catchError(this.handleError)
    );
  }

在您的组件中:

this.productService.getProducts().subscribe({
    next: (response : ServerResponse<ProductData>) => this.data = response?.data?.products,
    error: err => this.errorMessage = err
  });

Your service doesn't return ProductTableItem but the server response. Create two models:

export class ProductData {
  products?: ProductTableItem[]
}

and

export class ServerResponse<T> {
  timestamp: any;
  status: string;
  data: T;
}

Now in your service:

getProducts(): Observable<ServerResponse<ProductData>> {
  return this.http.get<ServerResponse<ProductData>>(this.productUrl)
    .pipe(
      tap(data => console.log('All', JSON.stringify(data))),
      catchError(this.handleError)
    );
  }

and in your component:

this.productService.getProducts().subscribe({
    next: (response : ServerResponse<ProductData>) => this.data = response?.data?.products,
    error: err => this.errorMessage = err
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文