【发布时间】:2021-04-17 16:53:55
【问题描述】:
我不知道这是一个错误还是只是一个误用,所以我在这里问......
在我看来:
<div>
<button wire:click="edit(1)"/>
@if ($updateMode)
Editing id {{ $productType->id }} / {{ $productType->name }}
<input wire:model="productType.id"/> <!-- renders nothing -->
<input wire:model="productType.name"/> <!-- renders the Product name correctly -->
@endif
</div>
在我的组件上:
use App\Models\ProductType;
use Livewire\Component;
class ProductTypes extends Component
{
public ProductType $productType;
public bool $updateMode;
public function mount()
{
$this->productType = new ProductType();
$this->updateMode = false;
$this->productTypes = ProductType::all();
}
public function edit($id)
{
$this->productType = $this->productTypes->find($id);
// dd($this->productType->id);
$this->updateMode = true;
}
/** ... */
}
您看到有一个dd($this->productType->id) 显示正确的信息。
但在视图上,它始终显示它创建的最后一个 ProductType。
通过使用<input wire:model="productType.name"/>,它会在输入字段中显示正确的信息,但使用{{ $product->name }} 时不会。
另一个注意事项是使用<input wire:model="productType.id"/>它无论如何都会显示空字段。
知道如何解决这个问题吗?
【问题讨论】: