以下自定义RxJS运算符(实际上只是演示的.filter等效运算符)当前在Angular 4.0.0-rc.2组件中声明 .
declare module 'rxjs/Observable' {
interface Observable<T> {
restrictToCommand<U>(f: (x: T) => U): Observable<U>;
}
}
Observable.prototype.restrictToCommand = function (cmd) {
return new Observable((observer) => {
const obs = {
next: (x) => {
if (x.command === cmd) {
observer.next(x);
}
},
error: (err) => observer.error(err),
complete: () => observer.complete()
};
return this.subscribe(obs);
});
};
这很好用 . 但是,我想将此声明提取到负责外部通信的库中 . 该库的主要导入是单件服务 .
如何从该库中正确导出原型扩展和模块声明?
1 回答
如果操作员不方便,它可以遵循RxJS与
rxjs/add/operator/...
一起使用的方式 .操作员定义可以移动到包根目录中的单独文件(
restrictToCommandOperator.js
和restrictToCommandOperator.d.ts
),并像应该注意的是,只有当包没有版本限制的依赖性时,这才能顺利运行 . 如果是,那么's a chance that there may be multiple RxJS package versions per user'的项目,
restrictToCommandOperator
将不会扩展所需的Observable
.