Laravel Livewire 3与Laravel Echo私有频道Dispatch失效问题
实时消息通知刷新失败问题
我正在构建新消息通知功能,希望将通知实时广播到客户端。当前代码已能广播新消息通知,但无法监听数据并刷新消息列表,控制台还出现如下错误。
发送通知代码
class NewMessage extends Notification implements ShouldQueue { use Queueable; private Message $message; private User $sender; public function __construct($mes) { $this->message=$mes; $this->sender=User::find($mes->from_id); } /** * 获取通知的投递渠道 * * @return array<int, string> */ public function via(object $notifiable): array { return['database','broadcast']; // return ['mail', 'database','broadcast']; } /** * 获取通知的邮件格式 */ public function toMail(object $notifiable): MailMessage { $url = url('/moncompte/mesmessages/'.$this->message->from_id); return (new MailMessage) ->subject('新消息通知') ->line('你收到了一条新消息。') ->action('查看消息', $url) ->line($this->message->body); } /** * 获取通知的数组格式(存储到数据库) * * @return array<string, mixed> */ public function toDatabase(object $notifiable): array { return [ 'id' => $this->message->id, 'from_id' => $this->sender->id, 'name' => $this->sender->name, 'content' => ' 给你发送了一条消息' ]; } public function toBroadcast(object $notifiable): BroadcastMessage { return new BroadcastMessage([ 'id' => $this->message->id, 'from_id' => $this->sender->id, 'name' => $this->sender->name, 'content' => ' 给你发送了一条消息' ]); } }
监听通知频道并触发刷新的代码
window.Echo.private(`App.Models.User.${userId}`) .notification((notification) => { console.log(notification.type); Livewire.dispatch('refreshData'); });
监听刷新事件的代码
#[On('echo-private:App.Models.User.{user.id}')] public function refreshData2() { dd('refresh2'); $this->notifications=$this->user->notifications->where('type','!=','App\Notifications\NewMessage'); $this->messages=$this->user->notifications->where('type','==','App\Notifications\NewMessage'); } #[On('refreshData')] public function refreshData() { dd('refresh'); $this->notifications=$this->user->notifications->where('type','!=','App\Notifications\NewMessage'); $this->messages=$this->user->notifications->where('type','==','App\Notifications\NewMessage'); }
控制台错误信息
Uncaught TypeError: 无法读取未定义属性(读取 'charAt') 在 e.value (app-c7bad5b3.js:5:10126) 在 n.value (app-c7bad5b3.js:5:10732) 在 livewire.js?id=8a199ab2:8657:46 在 Array.forEach (<anonymous>) 在 Array.<anonymous> (livewire.js?id=8a199ab2:8634:16) 在 trigger (livewire.js?id=8a199ab2:433:34) 在 Component.processEffects (livewire.js?id=8a199ab2:4371:7) 在 new Component (livewire.js?id=8a199ab2:4340:12) 在 initComponent (livewire.js?id=8a199ab2:4400:21) 在 livewire.js?id=8a199ab2:8267:26
内容的提问来源于stack exchange,提问作者Rumike7
相关产品推荐
相关产品推荐

