angular - 错误 TS2339:“可观察”类型上不存在属性“扫描”'
问题描述
我正在按照本教程制作带有角度的聊天机器人。我收到以下错误:
src/app/chat/chat-dialog/chat-dialog.component.ts(24,6) 中的错误:错误 TS2339:“可观察”类型上不存在属性“扫描”。
我正在使用 Angular 和 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 = '';
}
}
聊天服务.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]);
}
}
解决方案
scan
是需要管道的 Operator。
ngOnInit() {
this.messages = this.chat.conversation.asObservable()
.pipe(
scan((acc, val) => acc + val)
)
}
推荐阅读
- pyspark - 为什么 exp 函数在 pyspark 中不起作用?
- python - 使用 Beautiful soup(
- perl - apache2:Perl lib 版本 (5.28.1) 与可执行文件 '/usr/sbin/apache2' 版本 (5.20.2) 不匹配
- html - CSS 定位在父元素之外
- docxtemplater - Docxtemplater 可变数据键
- typescript - Ionic BLE 写入其他设备
- pine-script - 根据值绘制不同颜色的图表
- php - 使用 PHP 上传多个文件并在 CSS 中存储表单
- javascript - 如何在矩形(three.js)中切孔?
- matlab - 慢速数据游标回调期间的数据提示标签不正确