【发布时间】:2021-02-01 22:48:24
【问题描述】:
我在更改子组件时遇到嵌套组件问题 这是我的父组件,包括一个表格 parent.blade.php >>>
<table class="table mb-0">
<thead>
<tr>
<th scope="col">product</th>
<th>price</th>
<th>quantity</th>
</tr>
</thead>
<tbody>
@foreach ($cartItems as $item)
<tr>
<td>{{$item['name']}}</td>
<td>{{Cart::session(auth()->id())->get($item['id'])->getPriceSum()}}</td>
<td>
<livewire:child :item="$item" wire:key="$item['id']"/>
</td>
</tr>
@endforeach
</tbody>
</table>
这个id是parent.php
class Parent extends Component
{
public $service = null;
public $cartItems = [];
protected $listeners = ['cartUpdated' => 'onCartUpdate'];
public function mount()
{
$this->cartItems = \Cart::session(auth()->id())->getContent()->toArray();
}
public function onCartUpdate()
{
$this->mount();
}
public function render()
{
$this->mount();
return view('livewire.parent');
}
}
<td> 中的子组件只有一个带有livewire:model 的输入
<div>
<input type="number" wire:model="quantity" wire:change="updateCart"><span class="col-9"></span>
</div>
我的孩子.php
class Child extends Component
{
public $item;
public $quantity;
public function mount($item)
{
$this->item = $item;
$this->quantity = $item['quantity'];
}
public function updateCart()
{
\Cart::session(auth()->id())->update($this->item['id'], [
'quantity' => array(
'relative' => false,
'value' => $this->quantity,
),
]);
$this->emit('cartUpdated');
}
public function render()
{
return view('livewire.child');
}
}
我认为这会返回到孩子的mount() 方法。我可以每次更新数量吗?
【问题讨论】: