Broadcasted Laravel通知经Pusher推送控制台可见,前端无法实时展示求助
问题原因及修复方案
1. 通知推送逻辑错误
你的push逻辑存在两个明显问题:一是构造的新通知对象缺少id字段,而模板中v-for使用unread.id作为唯一key,key缺失会导致Vue无法检测到列表更新;二是手动拼接的data结构和后端返回的通知结构不一致,同时存在回调函数内this指向异常的风险。
修改Notification.vue的mounted代码:
mounted() { console.log('Component mounted.'); // 提前存储Vue实例,避免回调作用域内this指向错误 const vm = this; Echo.private('users.' + this.userid) .notification((notification) => { console.log(notification); // 构造和后端初始传入结构完全一致的通知对象 const newUnread = { id: notification.id, data: notification.data }; vm.unreadNotifications.push(newUnread); }); }
2. 通知项组件链接不更新
NotificationItem.vue中你把threadUrl放在mounted中赋值,而mounted仅会在组件首次创建时执行一次,后续新传入的unread属性不会触发mounted重新执行,导致新通知的链接生成失败。
将threadUrl改为计算属性即可:
export default { props:['unread'], computed: { threadUrl() { return "admin/patient/" + this.unread.data.patient.id } } }
删除原有data中定义的threadUrl和mounted中的赋值逻辑。
3. 可选校验项
确认Blade中传入的初始未读通知格式正确,Laravel集合转JSON时没有异常,同时删除冗余的onclick绑定避免冲突:
<notification :userid="{{auth()->id()}}" :unreads="{{auth()->user()->unreadNotifications->toArray()}}"></notification>
内容的提问来源于stack exchange,提问作者Vladimir B.
相关产品推荐
相关产品推荐

