Javascript Laravel Echo未触发回调

Javascript Laravel Echo未触发回调,javascript,laravel,pusher,laravel-echo,Javascript,Laravel,Pusher,Laravel Echo,我最近设置了我的Laravel应用程序,并将Echo配置为接收推送器通知。不幸的是,回调函数没有被触发,即使接收到了事件以及包含该事件的数据。您可以在下面找到我的代码: web.php Route::get('/pusher', function() { $location = ['lang' => 'langTets', 'lat' => 'latTest']; event(new Freight\Events\LocationUpdated($location))

我最近设置了我的Laravel应用程序,并将Echo配置为接收推送器通知。不幸的是,回调函数没有被触发,即使接收到了事件以及包含该事件的数据。您可以在下面找到我的代码:

web.php

Route::get('/pusher', function() {
    $location = ['lang' => 'langTets', 'lat' => 'latTest'];
    event(new Freight\Events\LocationUpdated($location));
    return "Event has been sent!";
});
bootstrap.js

import Echo from 'laravel-echo'

window.Pusher = require('pusher-js');
Pusher.logToConsole = true;

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'iscorrect',
    cluster: 'eu',
    encrypted: true
});

window.Echo.channel('location-updates')
    .listen('.LocationUpdated', function(location) {
        console.log("Test");
    });
我的应用程序刀片

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>Freight</title>

    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
    <!-- JS -->
    <script src="{{ asset('js/app.js') }}"></script>
</head>
<body>
...
现在,在触发事件后,控制台中会显示以下内容:

Pusher : Event recd : {"event":"Freight\\Events\\LocationUpdated","data":{"location":{"lang":"langTets","lat":"latTest"}},"channel":"location-updates"}
app.js:32652 Pusher : No callbacks on location-updates for Freight\Events\LocationUpdated

您知道console.log无法正常工作的原因吗?如果需要更多信息,请随时询问

我仍然没有找到问题的根源。然而,这完美地解决了这个问题:

  • 在事件中实现broadcastAs()方法,如下所示:
broadcastAs(){
返回“myneweventname”;
}

  • 更改.js文件,以便侦听新创建的事件别名(不带前导句点)
别忘了添加。在myneweventname Echo.channel('my-channel').listen('.myneweventname',函数(e){alert(e);}之前
Pusher : Event recd : {"event":"Freight\\Events\\LocationUpdated","data":{"location":{"lang":"langTets","lat":"latTest"}},"channel":"location-updates"}
app.js:32652 Pusher : No callbacks on location-updates for Freight\Events\LocationUpdated