【发布时间】:2019-01-01 01:18:23
【问题描述】:
我在我的节点服务器中创建了这个事件:
socket.on('hello' , function (data) {console.log('hello from the other side');})
在我的聊天组件上我放了:
constructor(private authService:AuthService,private router : Router) {
const socket=io('http://localhost:4000');
console.log(socket);}
onSendMessage(){
console.log('yep yep');
console.log(this.socket);
this.socket.emit('hello');
}
在我的聊天组件.html 上:
<div class="col-md-8">
<div class="chat" id="chat"> </div>
<form (ngSubmit)="onSendMessage()">
<div class="form-group">
<label>Enter Message</label>
<textarea class="form control" id="message"></textarea>
<br/>
<input type="submit" class="btn btn-primary" value="Send Message"/>
</div>
</form>
</div>
结果是这样的:
无法读取未定义的属性“发射” 在 ChatComponent.push../src/app/components/chat/chat.component.ts.ChatComponent.onSendMessage
这是 Web 控制台的图像:
PS:每当我点击发送消息时,套接字总是断开并重新连接
【问题讨论】:
标签: node.js angular sockets mean