我有一個列出用戶的簡單 Livewire 組件:
class UserListComponent extends Component
{
use WithPagination;
protected $listeners = ['refreshComponent' => '$refresh'];
public $search = '';
public $orderBy = 'id';
public $orderAsc = true;
public $perPage = 10;
public function render()
{
return view('livewire.admin.user-list-component',[
'users' => User::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'ASC' : 'DESC')
->simplePaginate($this->perPage)
]);
}
}
以及添加用戶的組件:
public function addUser()
{
// validate data
$validatedData = $this->validate();
// generate random password
$bytes = openssl_random_pseudo_bytes(4);
$password = bin2hex($bytes);
// create user
$user = User::create([
'name' => $validatedData['name'],
'email' => $validatedData['email'],
'password' => Hash::make($password),
]);
event(new Registered($user));
// assign user role
$user->attachRole('user');
$this->emitTo('UserListComponent', 'refreshComponent');
}
如您所見,在addUser()函式結束時,我向 UserListComponent 發出一個事件,并在該組件中設定了一個偵聽器來重繪 它,以便在添加用戶時用戶串列會自動更新。但是,它不起作用。如果我手動重繪 ,我可以看到用戶被添加到資料庫中并顯示得很好,但是組件重繪 沒有發生,也沒有拋出錯誤。
有任何想法嗎?
uj5u.com熱心網友回復:
從我看來,最好的方法是使用完整的類名,而不是:
$this->emitTo('UserListComponent', 'refreshComponent');
你應該使用:
$this->emitTo(\App\Http\Livewire\UserListComponent:class, 'refreshComponent');
(當然,如果您UserListComponent在其他命名空間中有更新命名空間)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/361732.html
