简体   繁体   English

Laravel / Livewire 在 laravel 混合编译 app.js 中不起作用

[英]Laravel / Livewire does not work in laravel mix complied app.js

I use Laravel with Livewire.我将 Laravel 与 Livewire 一起使用。 I wanted to react to an event in js.我想对 js 中的事件做出反应。 Once its emitted, an js alert should appear.一旦发出,应该会出现一个 js 警报。 The problem seems to be in app.js were I have my js function.问题似乎出在 app.js 中,如果我有我的 js function。 I receive an error, Livewire is not available.我收到一个错误,Livewire 不可用。 I use Laravel Mix to compile it, and it compiles all fine.我使用 Laravel Mix 来编译它,它编译一切正常。

Error: Uncaught TypeError: Livewire.on is not a function

app.js:应用程序.js:

import Livewire from '../../vendor/livewire/livewire/dist/livewire'; 
Livewire.on('testEvent', function () {
  alert("test");
});

It looks like an import problem.它看起来像一个进口问题。 I've removed the package and tried it again and I also thought its a timing problem, so I executed on document load but still the same problem.我已经删除了 package 并再次尝试,我也认为这是一个时间问题,所以我在文档加载时执行但仍然是同样的问题。 I've then decided to push it directly to the stack through a components blade file, and here it works all fine.然后我决定通过组件刀片文件将它直接推送到堆栈,在这里一切正常。

@push('scripts')
<script>
   Livewire.on('testEvent', function () {
   alert("test");
   });
</script>
@endpush

Pushing it to the stack should not be required as I've seen other code snippets without it and the error seems to be a simple import problem?不需要将其推送到堆栈,因为我已经看到其他代码片段没有它,并且错误似乎是一个简单的导入问题?

Use the browser Inspector and look if @livewireStyles and @livewireScripts are loaded in page.使用浏览器检查器并查看页面中是否加载了@livewireStyles 和@livewireScripts。 Be sure that Livewire already is installed确保已安装 Livewire

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

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