【问题标题】:How can I make rxjs-spy's let operator work?如何让 rxjs-spy 的 let 操作员工作?
【发布时间】:2020-11-11 02:09:58
【问题描述】:

如何使用rxjs-spylet() 方法更改可观察多次次的值?

我正在关注该工具的this introduction,当我想到使用 let() 的示例时: spy.let("people", source => source.mapTo("mallory")); 我被卡住了,因为source 是一个Observable<any>,它没有mapTo() 方法。因此,当粘贴到我的 chrome 控制台时,这条线应该正常工作,但由于 mapTo() 早已不复存在,它会引发异常。

我能想到的唯一方法不起作用。这允许我访问 rxjs 运算符

// In a top level typescript file (I add this file to debug builds only)
// This just lets me use rxjs operators, of, and tag, in the chrome console
import { tag } from 'rxjs-spy/operators/tag';
import { create } from 'rxjs-spy';
import * as operators from 'rxjs/operators';
import { of } from 'rxjs/observable/of';
window['debug'] = {
  rxjs: { operators, of },
  spy: { tag }
};
const spy = create();

然后我在某处有一个可观察的

myObservable.pipe(
  tag("foo")
)

在 chrome 控制台中。 (注意:我也试过不使用管道和标签)

spy.let("foo", fooObservable => window.debug.rxjs
    .of(1)
    .pipe(
        window.debug.spy.tag("foo")
    )
);

这仅在我第一次尝试更新值时有效。反正我觉得这工作量太大了。

我不理解的最后一个问题是在使用spy.let(...) 之后我认为spy.undo(1)(用当时正确的任何值替换 1)可能能够帮助我测试多个值,但它会破坏快照,给我类似的东西:

spy.show()
0 snapshot(s) matching /.+/

我在使用该工具时也经常收到此警告

index.js:3551 Cyclic next detected; type = subject; value = [object Object]; subscribed at
Observable._subscribe (http://localhost:4200/vendor.js:291983:33)
Observable.subscribe (http://localhost:4200/vendor.js:291934:22)
http://localhost:4200/vendor.js:291217:44
notify_ (http://localhost:4200/vendor.js:291076:9)

【问题讨论】:

    标签: angular rxjs rxjs6


    【解决方案1】:

    想通了——我只是错过了complete 选项。问题似乎是因为let() 默认completetrue

    所以我仍然有一个文件在我的调试版本中使用。这允许我在 chrome 控制台中使用 rxjs 运算符:

    // import { tag } from 'rxjs-spy/operators/tag'; You shouldn't need this in the console.
    import { create } from 'rxjs-spy';
    import * as operators from 'rxjs/operators';
    import { of } from 'rxjs/observable/of';
    window['debug'] = { rxjs: { operators, of } };
    const spy = create();
    

    然后在控制台中我将完整选项设置为false。根据您的操作,这可能不适用,但我想继续手动更新值以查看更改的结果。

    // Add { complete: false } to the parameter list.
    spy.let("foo", fooObservable => window.debug.rxjs.of(8), { complete: false });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-19
      • 2016-05-10
      • 2013-03-04
      • 2018-07-08
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多