简体   繁体   English

异步管道未处理Angular 2应用程序中预期的订阅

[英]Async Pipe Not Handling Subscription as Expected in Angular 2 App

I have been successfully using Observables throughout my Angular app. 我在Angular应用程序中成功使用了Observables。 Up until now I've been explicitly subscribing in the component, and then using *ngFor to iterate over an array of records in the view. 到目前为止,我已经在组件中明确订阅,然后使用* ngFor迭代视图中的记录数组。 Now I'd like to simplify things by using Angular's async pipe in the various views - so that will handle subscription and un-subscription. 现在我想通过在各种视图中使用Angular的异步管道来简化事情 - 这样就可以处理订阅和取消订阅。 So far this isn't working. 到目前为止,这是行不通的。

This is what I originally had (where I was subscribing in the component): 这就是我原来的(我在组件中订阅的地方):

ngOnInit() {
    this.clientService.getAll(1, this.pagesize)
        .subscribe(resRecordsData => {
            this.records = resRecordsData;
            console.log(this.records);
        },
        responseRecordsError => this.errorMsg = responseRecordsError);
}

And then in the view I was doing the following: 然后在视图中我做了以下事情:

<tr *ngFor="let record of records.data>

By the way, the data structure from the API looks like this (the records I am iterating over are within the "data" array, hence "records.data" in the *ngFor above): 顺便说一句,来自API的数据结构看起来像这样(我正在迭代的记录在“data”数组中,因此在上面的* ngFor中的“records.data”):

{
  "count": 15298,
  "data": [
    {

So, to move to using the async pipe, I changed my component ngOnInit call to this, where I'm explicitly declaring this to be an observable, and assigning it to a variable "records": 因此,为了转向使用异步管道,我将组件ngOnInit调用更改为this,其中我明确声明这是一个可观察的,并将其分配给变量“records”:

ngOnInit() {
    const records: Observable<any> = this.clientsService.getAll(1, this.pagesize);
    console.log(this.records);
}

I also brought the observable import from RxJs into the component, like so: 我还将来自RxJs的可观察导入带入了组件,如下所示:

import { Observable } from 'rxjs/Observable';

And then in the view I added the async pipe, like so: 然后在视图中我添加了异步管道,如下所示:

<tr *ngFor="let record of records.data | async">

As I mentioned this isn't working. 正如我所提到的,这是行不通的。 I'm not getting any console errors, I just don't see any data populated to the view. 我没有收到任何控制台错误,我只是没有看到填充到视图的任何数据。 Also, in the console, "records" is an empty array. 此外,在控制台中,“records”是一个空数组。

By the way, my service "getAll" call looks like this: 顺便说一句,我的服务“getAll”调用如下所示:

getAll(page, pagesize) {
    return this.http.get
    (`https://api.someurl.com/${this.ver}/clients?apikey=${this.key}&page=${page}&pagesize=${pagesize}`)
        .map((response: Response) => response.json())
        .catch(this.errorHandler);
}
    errorHandler(error: Response) {
    console.error(error);
    return Observable.throw(error.json().error || 'Server error');
}

I'm not sure what else is necessary to get the results to populate via subscription through the async pipe here. 我不确定还有什么必要通过这里的异步管道通过订阅填充结果。 Any ideas what's missing or in error with my code here? 我的代码在这里有什么缺失或错误的想法?

records$: Observable<any>;

ngOnInit() {
  this.records$ = this.clientService.getAll(1, this.pagesize);
}

in the view (assuming you are using Angular 4+): 在视图中(假设您使用的是Angular 4+):

<table *ngIf="records$ | async as records">
  <tr *ngFor="let record of records.data">

const records is a local variable. const records是一个局部变量。 You need to make it a field in order for template to have access to this variable. 您需要将其设为一个字段,以便模板可以访问此变量。

Ex: 例如:

public records: any;
ngOnInit() {
    this.records = this.clientsService.getAll(1, this.pagesize);
    console.log(this.records);
}

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

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