Laravel Livewire组件保存后不刷新
我陷入了我的感觉应该很容易的事情,但这只是不起作用。我将记录保存到数据库中的集合中,该集合显示为页面上的列表。因此,保存该记录后,我想自动更新页面以显示更新的列表,包括新记录。
刀片组件非常简单地循环循环。我已经删除了一些代码以简单,但是我知道这一切都必须位于一个Div元素中:
<div>
@foreach ($statements as $statement)
<div class="col-12 col-md-4 mb-2" wire:key="statement-{{ $statement->id }}">
...other stuff.
</div>
@endforeach
</div>
在我目前正在执行此操作的LiveWire组件中,这是不起作用的:
class VendorStatement extends Component
{
public Location $location;
public Statement $statement;
public $statements;
protected $listeners = [
'fileUploaded' => 'save',
'refreshComponent' => '$refresh',
];
public function mount(Location $location)
{
$this->location = $location;
$this->refreshStatements();
}
public function render()
{
return view('livewire.locations.statements');
}
public function refreshStatements()
{
$this->statements = $this->location->statements;
$this->render();
}
public function save($uploadedFile)
{
if ($path = Storage::disk('s3')->put(env('APP_ENV') . '/statements/' . $uploadedFile[1], file_get_contents($uploadedFile[0]))) {
$this->statement = new Statement([
'location_id' => $this->location->id,
'file_name' => $uploadedFile[1],
'path' => $path,
'uploaded_by' => Auth::user()->id,
]);
$this->statement->save();
}
StatementUploadedEvent::dispatch($this->statement);
$this->refreshStatements();
}
}
我' VE试验:
我尝试使用$ this-&gt; emit('RefReshComponent');
保存后而不是调用refreshstatements
方法。
我也尝试再次设置$ this-&gt;语句的值
。
不知道为什么它不起作用。
I'm stuck on what I feel like should be easy, but it's just not working. I'm saving a record to the database for the collection that's displayed as a list on the page. So after saving that record, I want to update the page to show the updated list, including the new record, automatically.
The blade component is pretty straight forward foreach loop over the collection. I've removed some of the code for simplicity, but I'm aware that it all must be in a single div element:
<div>
@foreach ($statements as $statement)
<div class="col-12 col-md-4 mb-2" wire:key="statement-{{ $statement->id }}">
...other stuff.
</div>
@endforeach
</div>
and in my Livewire component I'm doing this currently, which is not working:
class VendorStatement extends Component
{
public Location $location;
public Statement $statement;
public $statements;
protected $listeners = [
'fileUploaded' => 'save',
'refreshComponent' => '$refresh',
];
public function mount(Location $location)
{
$this->location = $location;
$this->refreshStatements();
}
public function render()
{
return view('livewire.locations.statements');
}
public function refreshStatements()
{
$this->statements = $this->location->statements;
$this->render();
}
public function save($uploadedFile)
{
if ($path = Storage::disk('s3')->put(env('APP_ENV') . '/statements/' . $uploadedFile[1], file_get_contents($uploadedFile[0]))) {
$this->statement = new Statement([
'location_id' => $this->location->id,
'file_name' => $uploadedFile[1],
'path' => $path,
'uploaded_by' => Auth::user()->id,
]);
$this->statement->save();
}
StatementUploadedEvent::dispatch($this->statement);
$this->refreshStatements();
}
}
Things I've Tried:
I've tried using $this->emit('refreshComponent');
after the save instead of calling the refreshStatements
method.
I've tried just setting the value of $this->statements
again as well.
Not sure why it isn't working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够通过像这样修改我的代码来使其起作用:
我仍然不明白为什么这不会按照我的配置方式工作,但至少它可以正常工作。
I was able to get this to work by modifying my code like so:
I still don't understand why this wouldn't work the way I had it configured, but at least it works.