错误TS2339:类型'Observable <message []>'上的属性'scan'不存在

时间:2018-05-24 11:26:25

标签: angular typescript rxjs

我正在关注这个tutorial用于制作带有角度的聊天机器人。我收到以下错误:

错误在src / app / chat / chat-dialog / chat-dialog.component.ts(24,6):错误TS2339:属性'scan'在类型'Observable'上不存在。

我正在使用Angular&amp; Angular CLI版本:6.0.3。

聊天dialog.component.ts

import { Component, OnInit } from '@angular/core';
import { ChatService, Message } from '../chat.service';
import { Observable } from 'rxjs';
import { scan } from 'rxjs/operators';




@Component({
  selector: 'chat-dialog',
  templateUrl: './chat-dialog.component.html',
  styleUrls: ['./chat-dialog.component.scss']
})
export class ChatDialogComponent implements OnInit {

  messages: Observable<Message[]>;
  formValue: string;

  constructor(public chat: ChatService) { }

  ngOnInit() {
    // appends to array after each new message is added to feedSource
    this.messages = this.chat.conversation.asObservable()
    .scan((acc, val) => acc + val);
  }

  sendMessage() {
    this.chat.converse(this.formValue);
    this.formValue = '';
  }

}

chat.service.ts

import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';

import { ApiAiClient } from 'api-ai-javascript';

import { Observable } from 'rxjs';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

// Message class for displaying messages in the component
export class Message {
  constructor(public content: string, public sentBy: string) {}
}

@Injectable()
export class ChatService {

  readonly token = environment.dialogflow.angularBot;
  readonly client = new ApiAiClient({ accessToken: this.token });

  conversation = new BehaviorSubject<Message[]>([]);

  constructor() {}

  // Sends and receives messages via DialogFlow
  converse(msg: string) {
    const userMessage = new Message(msg, 'user');
    this.update(userMessage);

    return this.client.textRequest(msg)
               .then(res => {
                  const speech = res.result.fulfillment.speech;
                  const botMessage = new Message(speech, 'bot');
                  this.update(botMessage);
               });
  }



  // Adds message to source
  update(msg: Message) {
    this.conversation.next([msg]);
  }

}

1 个答案:

答案 0 :(得分:3)

scan是需要通过管道传输的操作员。

  ngOnInit() {
    this.messages = this.chat.conversation.asObservable()
      .pipe(
        scan((acc, val) => acc + val)
      )
  }