1

在 Stomp 中,我们使用了这些方法:

initUserEvents() {

this.stompService.startConnect().then(() => {
      this.stompService.done('init');

this.stompService.subsribe('/channel/login', res => {
      if (res.username !== this.username) {

        this.newConnectedAccounts.push(res.username);

现在我正在使用 RxStomp,但我无法找出使用这些“替换”方法的正确方法:

initUserEvents() {

this.stompService.configure();
    this.stompService.activate(){

    this.stompService.watch('/channel/login', res => {
      if (res.username !== this.username) {
        this.newConnectedAccounts.push(res.username);

我得到的错误是: TS2345:'(res:any)=> void'类型的参数不可分配给'StompHeaders'类型的参数。类型“(res:any)=> void”中缺少索引签名。

4

1 回答 1

0

查看 RxStomp 文档。它说:

关键区别在于它将操作公开为 RxJS Observables。例如,当订阅 STOMP 端点时,它会返回一个 Observable,它将流式传输所有接收到的消息。

除了 beforeConnect,与@stomp/stompjs 客户端中所有回调相关的功能都公开为 Observables/Subjects/BehaviorSubjects。

换句话说,您不会将回调传递给该watch方法。它返回一个Observable你可以用来获取消息的。

RxStomp API 希望您将标头传递给watch方法,而不是回调。这就是为什么它专门告诉你:

TS2345: Argument of type '(res: any) => void' is not assignable to parameter of type 'StompHeaders'.
于 2020-11-20T14:59:22.590 回答