Reputation: 169
hope you are doing okay!
I'm using Laravel 8 and I'm getting the error Uncaught ReferenceError: Livewire is not defined please help me with how can I resolve that thank u.
\resources\views\livewire\crud-records-livewire.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
@livewireStyles
</head>
<body>
<div style="text-align: center">
<button wire:click="increment">+</button>
<h1></h1>
</div>
@livewireScripts
</body>
</html>
\app\Http\Livewire\CrudRecordsLivewire.php
class CrudRecordsLivewire extends Component
{
public $count = 0;
public function increment()
{
$this->count++;
}
public function render()
{
return view('livewire.crud-records-livewire');
}
}
Route::get('/messages',CrudRecordsLivewire::class);
Upvotes: 4
Views: 23746
Reputation: 11
Had the same issue, and publishing frontend assets didn't help for me either. Then noticed that I was referencing "Livewire" in a script tag before loading @livewireScripts. So make sure to first load the scripts, before referencing the Livewire Object.
Upvotes: 1
Reputation: 11
Try moving your laravel scripts into your web hosting root directory, (usually www, htdocs or public_html) and then retry. Oftentimes, livewire needs laravel to load from the root of the web hosting instead of a subdirectory. Check the attached image, if you see a similar error in your browser console you know this is the way to fix it.
Upvotes: 1
Reputation: 2954
Try to publish frontend assets
php artisan livewire:publish --assets
Upvotes: 16
Reputation: 4110
It seems to me that your hosting is off.
You should not see public in your URLs, but you can see Livewire scripts is trying to load the Livewire library from public/vendor/livewire
If you open the developer tools, I think you will see a 404 on the loading of the livewire script.
So you need to fix the hosting by making sure that the document root is the public folder, and you don't use some hacky tutorial that advises moving or editing index.php
as these are insecure and lead to other problems like this.
Upvotes: 3