Moh*_*lah 4 initialization laravel laravel-livewire alpine.js
我有一个使用 AlpineJS 隐藏的警报组件,但我希望它在 Livewire 重新渲染后再次可见。
使用 Livewire 组件显示警报
@if(Session::has('success'))
<x-success-alert :message="Session::get('success')" />
@endif
Run Code Online (Sandbox Code Playgroud)
AlpineJS 组件
<div x-data="{show: true}">
<div x-show="show">
<span>{{ $message }}</span>
<button @click="show = false">×</button>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
Moh*_*lah 16
The solution is to force Livewire to add that element in the dom again by adding wire:key to a random value.
<div wire:key="{{ rand() }}">
<div x-data="{show: true}">
<div x-show="show">
<span>{{ $message }}</span>
<button @click="show = false">×</button>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
With that way, Livewire will destroy the old dom element and add the new one which re-init the Alpine component.