首页 > 解决方案 > 如何在 Angular 中使用自定义管道覆盖默认的“异步”管道?

问题描述

我想做这样的事情:

import { AsyncPipe } from '@angular/common';
import { ChangeDetectorRef, Pipe } from '@angular/core';
import { PushPipe } from '@ngrx/component';
import { Observable } from 'rxjs';

@Pipe({
    name: 'async'
})
export class AsyncPushPipe<T> extends AsyncPipe {
    constructor(cdRef: ChangeDetectorRef, private pipe: PushPipe<T>) {
        super(cdRef);
    }

    transform<T>(obj: Observable<T> | Promise<T>): T | null;
    transform<T>(obj: null | undefined): null;
    transform<T>(obj: Observable<T> | Promise<T> | null | undefined): T | null {
        return this.pipe.transform(obj as any);
    }
}

我将它添加到我的应用程序模块中,但似乎没有任何代码正在执行。PLATFORM_PIPES 在 Angular 2 中被弃用,这被认为是通过将代码添加到应用程序的 NgModule 来覆盖默认提供程序的方式。

有小费吗?

注意:我知道这是一种反模式,但我想在决定移动到推管是否是一个好主意之前进行一些测量。

标签: angular

解决方案


推荐阅读