![](/img/trans.png)
[英]error TS2322: Type '() => string' is not assignable to type 'string'
[英]error TS2322: Type 'undefined' is not assignable to type 'string'
我至少正在尝试为展览开发一个聊天机器人,错误如下:
src/app/chat/chat.component.ts:32:9 - 错误 TS2322:类型“未定义”不可分配给类型“字符串”。 32 this.message = 未定义; ~~~~~~~~~~~~
并且代码部分如下所示
import { Component, OnInit } from '@angular/core';
import { IChat } from '../interfaces/ichat';
import { ChatService } from '../services/chat.service';
@Component({
selector: 'app-chat',
templateUrl: './chat.component.html',
styleUrls: ['./chat.component.css']
})
export class ChatComponent implements OnInit {
chats: IChat[] = [];
message: string;
sending: boolean;
constructor(private _chatService: ChatService) { }
ngOnInit() {
// subscribe to pusher's event
this._chatService.getChannel().bind('chat', data => {
if (data.email === this._chatService.user.email) {
data.isMe = true;
}
this.chats.push(data);
});
}
sendMessage(message: string) {
this.sending = true;
this._chatService.sendMessage(message)
.subscribe(resp => {
this.message = undefined;
this.sending = false;
}, err => {
this.sending = false;
} );
}
}
谢谢
您可以分配一个空字符串:
this.message = "";
代替
this.message = undefined;
或定义message
属性,如:
export class ChatComponent implements OnInit {
chats: IChat[] = [];
message: string | undefined;
然后你可以给它分配undefined
this.message = undefined;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.