【Laravel實戰】3分鐘搞懂Livewire的Defer Loading
Livewire 提供你 wire:init 語句讓你能在組件渲染完畢後立即執行行動。這在於你不想要載入整個頁面,而只想在頁面載入後載入一些資料時非常有用
//app/Http/Livewire/ShowPost.php
class ShowPost extends Component
{
public $readyToLoad = false;
public function loadPosts()
{
$this->readyToLoad = true;
}
public function render()
{
return view('livewire.show-posts', [
'posts' => $this->readyToLoad
? Post::all()
: [],
]);
}
}
//resources/views/livewire/show-post.blade.php
<div wire:init="loadPosts">
<ul>
@foreach ($posts as $post)
<li>{{ $post->title }}</li>
@endforeach
</ul>
</div>
loadPosts 行動 將會在 Livewire 組件被渲染在頁面上時立即被執行



