Pusher 未定义! Laravel 5.4 与 Laravel Echo

2024-01-10

我不知道我的代码有什么问题

这是我的app.js

/**
 * First we will load all of this project's JavaScript dependencies which
 * include Vue and Vue Resource. This gives a great starting point for
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

Vue.component('chat-message', require('./components/ChatMessage.vue'));
Vue.component('chat-log', require('./components/ChatLog.vue'));
Vue.component('chat-composer', require('./components/ChatComposer.vue'));

let idIsExist = document.getElementById('chat-init');

if(idIsExist !== null) {
    const app = new Vue({
        el: '#chat-init',
        http: {
            emulateJSON: true,
            emulateHTTP: true
        },
        data: {
            messages: []
        },
        methods: {
            addMessage(message) {
                axios.post('page-send-message', message).then(response => {
                    if(response.status !== 200) {
                        message = {
                            messages: response.statusText,
                            user: {
                                name: response.status
                            }
                        }
                    }
                    return this.messages.push(message);
                });
            }
        },
        created() {
            axios.get('page-messages').then(response => {
                this.messages = response.data;
            });

            Echo.join('page-chat').here().joining().leaving().listen('MessagePosted', e => {
                console.log(e);
            });
        }
    });
}

Here's my `bootstrap.js` 

window._ = require('lodash');

/**
 * We'll load jQuery and the Bootstrap jQuery plugin which provides support
 * for JavaScript based Bootstrap features such as modals and tabs. This
 * code may be modified to fit the specific needs of your application.
 */

window.$ = window.jQuery = require('jquery');
require('bootstrap-sass');

/**
 * Vue is a modern JavaScript library for building interactive web interfaces
 * using reactive data binding and reusable components. Vue's API is clean
 * and simple, leaving you to focus on building your next great project.
 */

window.Vue = require('vue');
require('vue-resource');

/**
 * We'll register a HTTP interceptor to attach the "CSRF" header to each of
 * the outgoing requests issued by this application. The CSRF middleware
 * included with Laravel will automatically verify the header's value.
 */

// Laravel Global Variable to use Laravel.csrfToken
let csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');

Vue.http.interceptors.push((request, next) => {
    request.headers.set('X-CSRF-TOKEN', csrfToken);
    next();
});

/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

window.axios = require('axios');

window.axios.defaults.headers.common = {
    'X-CSRF-TOKEN': csrfToken,
    'X-Requested-With': 'XMLHttpRequest'
};

这是我的bootstrap.js

/**
 * Echo exposes an expressive API for subscribing to channels and listening
 * for events that are broadcast by Laravel. Echo and event broadcasting
 * allows your team to easily build robust real-time web applications.
 */

import Echo from "laravel-echo"

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: '3c45e6945c69f616f4a3'
});

这是我的活动MessagePosted.php

<?php

namespace App\Events;

use App\User;
use App\Message;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class MessagePosted implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     *
     * @var Message
     *
     */

    public $message;

    /**
     *
     * @var User
     *
     */

    public $user;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Message $message, User $user)
    {
        $this->message = $message;
        $this->user = $user;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new PresenceChannel('page-chat');
    }
}

设置完成后,我在控制台中收到此错误

Uncaught ReferenceError: Pusher is not defined
    at PusherConnector.connect (eval at <anonymous> (app.js:345), <anonymous>:546:31)
    at PusherConnector.Connector (eval at <anonymous> (app.js:345), <anonymous>:192:14)
    at new PusherConnector (eval at <anonymous> (app.js:345), <anonymous>:537:135)
    at new Echo (eval at <anonymous> (app.js:345), <anonymous>:689:30)
    at eval (eval at <anonymous> (app.js:145), <anonymous>:59:15)
    at Object.<anonymous> (app.js:145)
    at __webpack_require__ (app.js:20)
    at eval (eval at <anonymous> (app.js:418), <anonymous>:8:1)
    at Object.<anonymous> (app.js:418)
    at __webpack_require__ (app.js:20)

我已经安装了独立性

composer require pusher/pusher-php-server
npm install --save laravel-echo pusher-js

你能帮忙解决我的问题吗?问题是 Pusher 没有定义,我不知道为什么。


在 bootstrap.js 文件中添加以下内容

import Pusher from "pusher-js"

此错误是由于 laravel 的更改所致,更多信息请参见此处https://github.com/laravel/echo/pull/110 https://github.com/laravel/echo/pull/110

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Pusher 未定义! Laravel 5.4 与 Laravel Echo 的相关文章

随机推荐

  • 如何使用 EF4 Fluent API 将删除级联到链接表中?

    我在现有 MSSQL 2008 R2 数据库中有两个表 它们通过链接表关联 这两个表是 计划 和 提示 链接表是 PlanTipLinks 计划可以有许多提示 并且提示可以与多个计划关联 即 这是多对多的关系 在应用程序中 我只关心 Pla
  • 如何在wpf中使富文本框自动调整大小?

    如何让RichTextBox自动调整大小 我希望我的富文本框能够适合我分配给它的任何文本 而无需滚动条 谢谢 Set the HorizontalAlignment and VerticalAlignment除了Stretch 这是默认值
  • 如何向 iso 8583 消息添加使用 jpos 的标头。 - ISO 8583 - jpos

    我是使用 iso 8583 进行开发的新手 我需要在 iso 消息之前添加标头 但我以这种方式实现它们 并且不会将其添加到消息标头中 我究竟做错了什么 我希望你可以帮助我 GenericPackager packager new Gener
  • 异步写入文件

    有没有办法编写一个异步函数来重复将数据写入文件 当我编写异步函数时出现以下错误 该进程无法访问文件 c Temp Data txt 因为该文件正在被另一个进程使用 public void GoButton Click object send
  • 如何为

    我只有 HTML 和 CSS 的基本知识 并且广泛地浏览了 Google 试图找到这个问题的答案 这一切似乎都指向 JavaScript 和 或 jQuery 我试过了 但无法让它工作 我有一个音频文件 在我的网站加载时开始播放 我想设置一
  • 如何将

    转换为换行符?

    假设我有一个 HTML p and br 里面有标签 之后 我将剥离 HTML 以清理标签 我怎样才能把它们变成换行符 我正在使用Python的美丽汤 http www crummy com software BeautifulSoup 图
  • 如何在 REST 中处理 @OneToMany 关系

    我正在设计一个小REST允许执行一些基本操作的应用程序 到目前为止一切顺利 我有以下 Entity called Client需要与 Entity called Loan Client Entity Table name clients p
  • 升级到 PyMongo 3.0 导致 ServerSelectionTimeoutError

    我最近将 Bottle uWSGI Nginx 应用程序升级到 MongoDB 3 0 2 它在 PyMongo 2 8 上运行良好 但今天我通过运行以下命令升级到 PyMongo 3 0 pip install upgrade pymon
  • 三次 Hermite 样条插值 python

    我想计算一个三次多项式 该多项式由其函数值和指定点的导数定义 https en wikipedia org wiki Cubic Hermite spline https en wikipedia org wiki Cubic Hermit
  • 如何计算正确的 apkCertificateDigestSha256 有权访问证书?

    SafetyNet 文档 https developer android com training safetynet attestation https developer android com training safetynet a
  • “转到定义”始终位于 VSCode 的新选项卡中

    我在 MAC BigSur 上安装了 VSCode 每当我单击 转到定义 时 它应该以new标签 如何实现这一目标 目前 仅当定义存在于另一个文件中时 定义才会在新选项卡中打开 否则 如果存在于同一文件中 它将滚动到同一文件中不需要的定义
  • Foursquare API 用于解决场地用户图像错误

    Foursquare API 已将用户的照片标签分为前缀和后缀 但是 如果我将它们合并以形成完整的图像 URL 并将其粘贴到浏览器中 则会出现错误 提示图像无法显示 因为它包含错误 是服务器暂时不可用还是其他原因 我正在使用 API 获取场
  • 将 editorfor 转换为下拉列表时出现问题

    请在这里查看达林的解决方案 将 HTML EditorFor 转换为下拉列表 html dropdownfor https stackoverflow com questions 9517627 converting html editor
  • 为什么 TOP 或 SET ROWCOUNT 使我的查询如此慢?

    我有一个包含大约 1400 万行的 SQL Server 2008 数据库 里面有两张桌子 Table1 rowId int primary key someData1 int someData2 int Table2 id int pri
  • 有没有办法使用 CSS3 渐变并回退到 PNG?

    如何将 CSS3 渐变用于背景图像 并在不支持渐变的情况下回退到 PNG 图像 干得好 不支持 CSS3 渐变的浏览器将仅使用图像 div background color 1a82f7 fallback color background
  • javascript倒计时并显示毫秒

    我想要倒计时 并希望显示类似分钟 秒 毫秒的格式 我用jquery插件倒计时了倒数 http keith wood name countdown html但它只显示分钟 秒格式 有什么办法可以纠正吗 非常感谢 大家好 我已经为自己开发了一个
  • 使用 ruby​​ 流式传输并解压大型 csv 文件

    我遇到问题 需要下载 解压缩 然后逐行处理一个非常大的 CSV 文件 我认为让您了解文件有多大很有用 big file zip 700mb big file csv 23gb 这是我希望发生的一些事情 解压前不必下载整个文件 在解析 csv
  • React:将组件状态值复制到剪贴板,无需虚拟元素

    在我的项目中 有一个用例 用户单击一个按钮 然后将一些数据复制到剪贴板以进行下一步 复制的数据与点击的按钮相关 并存储在组件状态中 我做了一些搜索 找到了可能的解决方案 如下所示 function copyToClipboard text
  • 限制模板实例化中的参数类型

    如果我的库的用户尝试使用不合适的类型实例化模板 我将尝试触发编译时错误 我已经实施了 template
  • Pusher 未定义! Laravel 5.4 与 Laravel Echo

    我不知道我的代码有什么问题 这是我的app js First we will load all of this project s JavaScript dependencies which include Vue and Vue Reso