简体   繁体   English

ReferenceError:未定义回声

[英]ReferenceError: Echo is not defined

I'm using laravel echo with pusher js to build a real-time feature in my app.我正在使用 laravel echo 和 pusher js 在我的应用程序中构建实时功能。 I've done all the steps required as told by the doc .我已经完成了文档所要求的所有步骤。 I've successfully set up the pusher configurations and it works fine: when my event is fired I get it notified in my Debug console on my Pusher account.我已经成功设置了推送器配置并且工作正常:当我的事件被触发时,我会在我的推送器帐户的调试控制台中收到通知。

My issue is with the front-end.我的问题是前端。 As told by the docs I've added the portion of code neccessary at the bottom of my resources/js/bootstrap.js file.正如文档所说,我在resources/js/bootstrap.js文件的底部添加了必要的代码部分。 I uncommented it actually and add the proper ids.我实际上取消了注释并添加了正确的ID。

import Echo from 'laravel-echo';

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

window.Echo = new Echo({
    broadcaster: 'pusher',
    key:my-pusher-key-here,
    cluster: eu,
    forceTLS: true
});

The issue is that although I've inserted those问题是虽然我已经插入了那些

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> 
<script src="js/echo.js"></script>
<script src="js/app.js"></script>
<script src="https://js.pusher.com/4.1/pusher.min.js"></script>
<script>
var channel = Echo.channel('my-channel');
channel.listen('my-event', function(data) {
  alert(JSON.stringify(data));
});
</script>

in my blade view, I'm getting this error in the browser's console: ReferenceError: Echo is not defined .在我的刀片视图中,我在浏览器的控制台中收到此错误: ReferenceError: Echo is not defined I'm guessing it has something to do with echo library not found but I'm not understanding why.我猜这与找不到回声库有关,但我不明白为什么。 For the <script src="js/echo.js"></script> line, I copied the echo.js file got after the npm install --save laravel-echo pusher-js command run.对于<script src="js/echo.js"></script>行,我复制了在npm install --save laravel-echo pusher-js命令运行后得到的 echo.js 文件。

I went through many suggestions like gulp to get the changes in the Js files automatically compiled I guess but nothing... For the gulp command entered in my CLI, I'm getting No gulpfile file found error.我经历了许多建议,例如gulp以获取自动编译的 Js 文件中的更改我猜但什么都没有...对于在我的 CLI 中输入的gulp命令,我收到No gulpfile file found错误。 I've also tried to copy the first portion of code directly in the app.js file instead but no difference, I'm getting the same error.我也尝试直接在app.js文件中复制代码的第一部分,但没有区别,我得到了同样的错误。

As I've said.. I guess this has certainly something to do with the Echo library not found but why that?正如我所说.. 我想这肯定与未找到的 Echo 库有关,但为什么呢? Your help will be very appreciated您的帮助将不胜感激

package.json file package.json文件

    "private": true,
    "scripts": {
        "build": "webpack -p",
        "watch": "npm run development -- --watch",
        "dev": "npm run development",
        "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    },
    "devDependencies": {
        "axios": "^0.19",
        "bootstrap": "^4.1.0",
        "cross-env": "^5.1",
        "jquery": "^3.2",
        "laravel-mix": "^4.0.7",
        "lodash": "^4.17.13",
        "popper.js": "^1.12",
        "resolve-url-loader": "^2.3.1",
        "sass": "^1.15.2",
        "sass-loader": "^7.1.0",
        "vue": "^2.5.17",
        "vue-template-compiler": "^2.6.10",
        "webpack": "^4.39.3",
        "webpack-cli": "^3.3.7"
    },
    "dependencies": {
        "gulp": "^4.0.2",
        "laravel-echo": "^1.5.4",
        "pusher-js": "^5.0.0"
    }
}

You have import Echo from 'laravel-echo';你已经import Echo from 'laravel-echo'; inside resources/js/bootstrap.js file.resources/js/bootstrap.js文件中。

It tries to search for Echo dependency in node_modules folder.它尝试在node_modules文件夹中搜索 Echo 依赖

You should use laravel-mix to build JavaScript files.你应该使用laravel-mix来构建 JavaScript 文件。

I have faced the same error as you, the error is happening in your resources/js/app.js file.我遇到了和你一样的错误,错误发生在你的 resources/js/app.js 文件中。 if you have commented require('./bootstrap');如果你评论过require('./bootstrap'); uncomment it it will work for you.取消注释它会为你工作。

I believe you messed up the steps after uncommenting the lines in resources/js/bootstrap.js file.我相信您在取消对resources/js/bootstrap.js文件中的行进行注释后搞砸了这些步骤。 To use the code in there you need to import the resources/js/app.js file.要使用其中的代码,您需要导入resources/js/app.js文件。 Go look in that, you'll see it is importing the bootstrap file.去看看,你会看到它正在导入引导文件。 You can import axios in your resources/js/app.js as you might need it in your blade.您可以在您的resources/js/app.js /js/app.js 中导入 axios,因为您可能在刀片中需要它。 Confirm that your resources/js/app.js file at least has these lines:确认您的resources/js/app.js文件至少包含以下resources/js/app.js行:

require('./bootstrap');
const { default: axios } = require('axios');
window.Vue = require('vue').default;

Then in your blade file use this to get vue running:然后在您的刀片文件中使用它来运行 vue:

<script src="{{ asset('js/app.js') }}"></script>
<script>
    new Vue({
        el: '#app',
        data: {},
        methods: {},
        mounted() {},
    });
</script>

Finally check the webpack.mix.js file and confirm that it at least has:最后检查webpack.mix.js文件并确认它至少有:

mix.js('resources/js/app.js', 'public/js')
.vue();

For me, using php 7.4 I had to run npm run production to build the js files.对我来说,使用 php 7.4 我必须运行npm run production来构建 js 文件。 And it was running flawlessly.它运行完美无缺。

Remove defer from your script tag.从您的脚本标签中删除 defer。 This will cause the app.js script to be deferred, and the other script will run first before this script这将导致 app.js 脚本被延迟,并且另一个脚本将在此脚本之前先运行

<!-- Scripts -->
<script src="{{ asset('js/app.js') }}" defer></script>

Go in terminal, Go 在终端,

tape this command: npm install and use laravel mix: npm run dev that resolve my error.磁带此命令: npm install和使用 laravel 混合: npm run dev解决我的错误的开发。

Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM