首页 文章

angular添加新的可观察运算符

提问于
浏览
0

基于来自该网站的信息How To Debug RxJs - A Simple Way For Debugging Rxjs Observables我创建了一个名为 observable-debug-operator.ts 的新文件,我在我的应用程序组件中导入 import './observable-debug-operator';

observable-debug-operator文件的内容如下

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/do';

import { environment } from '../environments/environment';

// add debugging to Observables
Observable.prototype.debug = function (message: string) {
  return this.do(
    function (next) {
      if (environment.observableDebugging) {
        console.log(message, next);
      }
    },
    function (err) {
      if (environment.observableDebugging) {
        console.error('ERROR >>> ', message , err);
      }
    },
    function () {
      if (environment.observableDebugging) {
        console.log('Completed.', message);
      }
    }
  );
};
declare module 'rxjs/Observable' {
  interface Observable<T> {
    debug: (...any) => Observable<T>;
  }
}

这是按预期工作的,我可以订阅一个observable并将其调试到控制台

this.service.methodThatReturnsObservable()
  .debug('doing something with an observable')
  .subscribe(...do something...);

然而,当我运行我的业力测试时,我得到了错误

TypeError: Cannot read property 'debug' of undefined

要么

Failed: Uncaught (in promise): TypeError: Cannot read property 'debug' of undefined
TypeError: Cannot read property 'debug' of undefined

我尝试在我的测试规范中包含 import 'the/path/to/observable-debug-operator'; 但我仍然得到错误 . 有谁知道为什么在测试中找不到调试操作符但在实际代码中工作正常?

1 回答

  • 0

    错误并不意味着您的 debug 未定义 . 它表示您的服务方法或Observable.prototype是 undefined . 所以检查它们是否都可用 .

相关问题