angular – TypeScript模块扩充

前端之家收集整理的这篇文章主要介绍了angular – TypeScript模块扩充前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有可观察的扩展.它工作得很好,但现在我已经更新到角度为6的打字稿2.7.2.

import { Observable } from 'rxjs/Observable';
import { BaseComponent } from './base-component';
import { Subscription } from 'rxjs/Subscription';
import { Subscribable } from 'rxjs';

declare module 'rxjs/Observable' {
    export interface Observable<T> {
        safeSubscribe<T>(this: Observable<T>,component: BaseComponent,next?: (value: T) => void,error?: (error: T) => void,complete?: () => void): Subscription;
    }
}


export function safeSubscribe<T>(this: Observable<T>,complete?: () => void): Subscription {
    let sub = this.subscribe(next,error,complete);
    component.markForSafeDelete(sub);
    return sub;
}

Observable.prototype.safeSubscribe = safeSubscribe;

这段代码不起作用

>’Observable’仅指类型,但在此处用作值.
>’Observable’类型中不存在属性’subscribe’.

https://www.typescriptlang.org/docs/handbook/declaration-merging.html

解决方法

合并声明时,指定的模块路径必须与实际模块的路径完全匹配.

使用RxJS版本6,您将需要更改模块声明,因为内部结构已更改.从记忆中,它应该是:

declare module 'rxjs/internal/Observable' {
    export interface Observable<T> {
        safeSubscribe<T>(this: Observable<T>,complete?: () => void): Subscription;
    }
}

有关示例,请参阅rxjs-compat中的one of the patching imports.

猜你在找的Angularjs相关文章