如何解决如何在Rails Action Cable订阅中调用角材料组件接收方法
我正在尝试在角度应用中实现Rails Action电缆。
从服务器广播消息时,在角度ActioncableService使用者中接收到数据。
但是当调用MatSnackBar实例显示消息时,它在订阅接收方法中是未定义的。
建议在MatSnackBar中显示服务器消息的正确方法
ActionCable.server.broadcast "web_notifications",content: "msg from server to angular"
这是我的ActioncableService代码
import { Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { createConsumer } from '@rails/actioncable';
@Injectable({
providedIn: 'root'
})
export class ActioncableService {
private consumer: any;
constructor(private matSnackBar: MatSnackBar) { }
webNotificationChannel(): void {
this.consumer = createConsumer('ws://localhost:7002/cable');
this.consumer.subscriptions.create({ channel: 'WebNotificationsChannel' },{
connected: this.webNotifcationChannelConnected,received: this.webNotifcationChannelReceived,});
}
webNotifcationChannelConnected(): void {
console.log('Web Notifications Channel Connected');
}
webNotifcationChannelReceived(data: any): void {
console.log(data); // Able to received data from server
const content = data.content;
this.matSnackBar.open(content,'Info',{
duration: 10 * 1000,horizontalPosition: 'end',verticalPosition: 'top'
});
}
}
控制台错误
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。