admin管理员组文章数量:1394400
How can I prevent my Livewire component from reloading every time the page refreshes? I placed the component in the resources\views\layouts\app.blade.php
file. This component is responsible for loading categories, and I want it to appear on every page, which is why I placed it there.
I also have a Product Livewire component and a Product Detail Livewire component. Does anyone have any ideas on how I can prevent the component from reloading? Thanks in advance for your help.
In the config\livewire.php
file, I set the layout as follows:
'layout' => 'layouts.app',
I include it in the app.blade.php
file like this:
@livewire('category-navigation')
resources\views\layouts\app.blade.php
<main>
<div class="container mx-auto mt-8 grid grid-cols-12 gap-6">
<div class="col-span-3">
@livewire('category-navigation')
</div>
<div class="col-span-9">
{{ $slot }}
</div>
</div>
</main>
Here is how I define the Category component (app\Livewire\CategoryNavigation.php
):
<?php
namespace App\Livewire;
use Livewire\Component;
use App\Models\Category;
class CategoryNavigation extends Component
{
public $categories;
public $currentCategory;
protected $listeners = ['category-selected' => 'setCategory'];
public function mount($selectedCategory = null)
{
$this->categories = Category::whereNull('parent_id')->where('status', 1)->get();
$this->currentCategory = $selectedCategory;
}
public function setCategory($slug)
{
$this->currentCategory = $slug;
}
public function navigateToCategory($fullPath)
{
return redirect($fullPath);
}
public function render()
{
return view('livewire.category-navigation', [
'categories' => $this->categories,
'currentCategory' => $this->currentCategory
]);
}
}
How can I prevent my Livewire component from reloading every time the page refreshes? I placed the component in the resources\views\layouts\app.blade.php
file. This component is responsible for loading categories, and I want it to appear on every page, which is why I placed it there.
I also have a Product Livewire component and a Product Detail Livewire component. Does anyone have any ideas on how I can prevent the component from reloading? Thanks in advance for your help.
In the config\livewire.php
file, I set the layout as follows:
'layout' => 'layouts.app',
I include it in the app.blade.php
file like this:
@livewire('category-navigation')
resources\views\layouts\app.blade.php
<main>
<div class="container mx-auto mt-8 grid grid-cols-12 gap-6">
<div class="col-span-3">
@livewire('category-navigation')
</div>
<div class="col-span-9">
{{ $slot }}
</div>
</div>
</main>
Here is how I define the Category component (app\Livewire\CategoryNavigation.php
):
<?php
namespace App\Livewire;
use Livewire\Component;
use App\Models\Category;
class CategoryNavigation extends Component
{
public $categories;
public $currentCategory;
protected $listeners = ['category-selected' => 'setCategory'];
public function mount($selectedCategory = null)
{
$this->categories = Category::whereNull('parent_id')->where('status', 1)->get();
$this->currentCategory = $selectedCategory;
}
public function setCategory($slug)
{
$this->currentCategory = $slug;
}
public function navigateToCategory($fullPath)
{
return redirect($fullPath);
}
public function render()
{
return view('livewire.category-navigation', [
'categories' => $this->categories,
'currentCategory' => $this->currentCategory
]);
}
}
Share
Improve this question
asked Mar 15 at 14:48
FzolFzol
211 silver badge6 bronze badges
1 Answer
Reset to default 0How can I prevent my Livewire component from reloading every time the page refreshes?
This question doesn't make any sense since the whole page get reloaded if you refresh (including your livewire component). But if you mean that you want to speed up the loading time of the component and avoid triggering a database request at every page reload, you can use the Cache facade from Laravel.
Here's an example :
class CategoryNavigation extends Component
{
public $categories;
public $currentCategory;
protected $listeners = ['category-selected' => 'setCategory'];
public function mount($selectedCategory = null)
{
$this->loadCategories();
$this->currentCategory = $selectedCategory;
}
public function loadCategories(): void
{
$this->categories = Cache::remember('categories', function() {
return Category::whereNull('parent_id')->where('status', 1)->get();
})
}
I hope this helps
本文标签: Livewire 3X and Laravel 12XPreventing Component ReloadingStack Overflow
版权声明:本文标题:Livewire 3.X and Laravel 12.X - Preventing Component Reloading - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744612206a2615724.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论