简体   繁体   English

* ngFor通过异步管道进行本地分配并减少了订阅数

[英]*ngFor with local assignment with the async pipe and reducing number of subscriptions

I have code similar to the following in the template file of a custom Angular component I have written: 我编写的自定义Angular组件的模板文件中具有与以下代码类似的代码:

<item
    [class.disabled]="domain?.disabled | async"
    [class.hidden]="!(domain?.subscribed | async) && !showHiddenDomains || (domain?.deleted | async)"
    [disabled]="domain?.disabled | async"
    [hideHealth]="true"
    *ngFor="let domain of (forest?.domains | async)"
    [status]="domain?.status | async"
    (expand)="collapseDomain($event, domain)">
    <span header>
        <div [class.container-disabled]="!(domain?.disabled | async) && !(domain?.iconType | async)">
            <spinner-2 [promise]="spinnerPromise" *ngIf="domain?.disabled | async"></spinner-2>
            <font-icon class="domain-header-icon-{{domain?.statusString | async}}" [type]="domain?.iconType | async" *ngIf="!(domain?.disabled | async)"></font-icon>
        </div>
        <div>{{domain.name}}</div>
    </span>
    .
    .
    .

I've been reading this article and am wondering if I can take advantage of the *ngIf with local assignment discussed at the bottom of the page. 我一直在阅读文章,我想知道如果我可以利用的*ngIf with local assignment在页面底部的讨论。 I read this: 我读到这个:

Here we are creating a local template variable that Angular assigns the value from the Observable. 在这里,我们创建一个本地模板变量,Angular从Observable分配值。 This allows us to interact directly with our user Object without having to use the async pipe over and over. 这使我们可以直接与用户对象进行交互,而不必反复使用异步管道。

On this page and was wondering if I could take advantage of this so that I do not have to keep reusing the async pipe over and over again as I feel it is drastically reducing performance on my page. 页面上,我想知道是否可以利用此优势,这样我就不必一遍又一遍地重复使用异步管道,因为我认为这会大大降低页面的性能。

Thoughts? 有什么想法吗? Thanks 谢谢

UPDATE 更新

I've been able to reduce most of the code to this fashion by wrapping ng-containers around blocks of code, for example: 通过将ng-containers包裹在代码块周围,我已经能够将大多数代码减少为这种方式,例如:

<ng-container *ngIf="forest?.domains | async as domains">
    <div *ngFor="let domain of domains">
        .
        .  
        .
    </div>
</ng-container>

Some of these domains have properties attached to them that are in the form of: 其中一些域具有以下形式的附加属性:

property: Observable.from([true]);

Anyone know what the above does? 有人知道以上内容吗?

Assuming that each of the domain object in the domains are subscribable entries. 假设每个的domain在对象domains是可订阅的条目。

Just create a component called ItemHeader as below and dumb down the subscriptions into one. 只需如下创建一个名为ItemHeader的组件,然后将订阅简化为一个。

Change your html as below 如下更改您的html

<item
    [class.disabled]="domain?.disabled | async"
    [class.hidden]="!(domain?.subscribed | async) && !showHiddenDomains || (domain?.deleted | async)"
    [disabled]="domain?.disabled | async"
    [hideHealth]="true"
    *ngFor="let domain of (forest?.domains | async)"
    [status]="domain?.status | async"
    (expand)="collapseDomain($event, domain)">
    <item-header [domain]="domain | async"></item-header>
    .
    .
    .

//item-header.component.ts //item-header.component.ts

@Component({
  selector: 'item-header',
  templateUrl: './item-header.component.html'
})
export class ItemHeaderComponent implements OnInit {
 @Input() domain: Domain 
 constructor(){}

}

//item-header.component.html //item-header.component.html

<span header>
        <div [class.container-disabled]="!(domain?.disabled | async) && !(domain?.iconType | async)">
            <spinner-2 [promise]="spinnerPromise" *ngIf="domain?.disabled | async"></spinner-2>
            <font-icon class="domain-header-icon-{{domain?.statusString | async}}" [type]="domain?.iconType | async" *ngIf="!(domain?.disabled)"></font-icon>
        </div>
        <div>{{domain.name}}</div>
    </span>

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

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