在更新数量时,我对显示更新有问题。在表中的数字旁边有左/右箭头,允许用户更改数字的值,如下所示:

当用户单击左侧按钮时,Livewire操作将触发将关联从属于多个关系中移除的操作。当用户单击正确的按钮时,Livewire操作将触发将关联添加到属于多个关系的关联。
然后,Livewire操作刷新模型,以便更新Livewire组件的动态模型属性,该属性计数关联记录的数量。
当数字达到0时,左箭头消失。当数字达到可用的总量时,正确的箭头就消失了。
这个组件的所有功能都很好,除了每当值将从0更改为1或从1更改为0时,数字就从显示中消失:
切换到0

切换到1

组件中的其他所有功能都完美无缺地工作,包括依赖于消失的值的其他功能。与组件中的任何其他操作交互会使数字神奇地重新出现。
如果我在刀片中添加了一个<?php dump($widget->my_associated) ?>,那么每次都会转储正确的值。参见从切换到1或4的示例:

这是我的组件的代码:
shop-widgets.blade.php
<td align="center">
<span style="white-space:nowrap;">
@if ($widget->my_associated)
<a href="#" wire:click="release(false)"><i class="fas fa-fw fa-caret-left"></i></a>
@else
<i class="fas fa-fw"></i>
@endif
{{ $widget->my_associated }}<?php dump($widget->my_associated) ?>
@if ($widget->remaining)
<a href="#" wire:click="reserve(false)"><i class="fas fa-fw fa-caret-right"></i></a>
@else
<i class="fas fa-fw"></i>
@endif
</span>
</td>ShopWidgets.php
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Widget;
class ShopWidgets extends Component
{
public $widget;
public function mount(Widget $widget)
{
$this->widget = $widget;
}
public function render()
{
return view('livewire.shop-widgets', [
'widget' => $this->widget,
]);
}
public function reserve($bought) {
$this->widget->reserve($bought);
$this->widget->refresh();
}
public function release($bought) {
$this->widget->release($bought);
$this->widget->refresh();
}
}Widget.php
public function reserve($bought)
{
Gate::authorize('reserve', $this);
if ($this->remaining > 0) {
$this->buyers()->attach(Auth::user()->id, ['bought' => $bought]);
}
}
public function release($bought)
{
Gate::authorize('release', $this);
$pivot = $this->buyers()->newPivotStatementForId(Auth::user()->id)->where('bought', $bought);
if ($pivot->count() > 0)
{
$pivot->limit(1)->delete();
}
}知道在这种非常特殊的情况下是什么导致了显示问题吗?
发布于 2020-11-25 13:51:56
因此,在再玩一遍之后,我意识到转储可能是正确显示的,因为它在<div>中。所以,我把数字显示放在一个<span>里,它似乎解决了我的问题.<span>{{ $widget->my_associated }}</span>
如果有人知道为什么会发生这种事,我很想知道如何解决未来的问题。
https://stackoverflow.com/questions/64998126
复制相似问题