php - 使用 Laravel Livewire 进行阵列管理和预览
问题描述
当使用输入文件通过 Livewire 加载图像时,我发现如果用户单击以加载多个图像,它可以正常工作,但是如果他第二次单击相同的输入,使用 Livewire 的预览将被选定的图像替换。第二次。为了解决这个问题,添加一个加载图像的数组,如下所示:
我的输入文件
<input wire:change="cargaImagenes" wire:model="imagen" type="file" name="imagen" accept="image/*" class="form-control-file" multiple>
活动费用
public function cargaImagenes()
{
foreach ($this->imagen as $ima) {
array_push($this->imagenes, $ima);
}
}
问题是我使用 Livewire 通过以下方式预览图像:
@if ($imagenes)
<small>previsualización:</small>
<div class="form-inline">
@foreach($imagenes as $img)
<div wire:key="{{$loop->index}}">
<div class="m-2 img-thumbnail">
<img class="my-2 rounded img-fluid contenedor-img" src="{{ $img->temporaryUrl() }}">
<button class="btn btn-outline-danger" wire:click="eliminarImagen({{ $loop->index }})">
</button>
</div>
</div>
@endforeach
<br>
</div>
@endif
问题是它们不会在我第一次选择图像时显示,它会在我添加更多图像时显示。
对于预览,我使用数组的 $images 属性
array_push($this->imagenes, $ima);
你能给我一个关于我可以做些什么来显示数组中的所有图像的建议吗?
解决方案
这应该有效。
拆线:彻底改变。
<input wire:model="imagen" type="file" name="imagen" accept="image/*" class="form-control-file" multiple>
挂钩 updatedImagen() 事件方法:
public function updatedImagen()
{
foreach ($this->imagen as $ima) {
$this->imagenes[] = $ima;
}
}
推荐阅读
- javascript - 在分配变量后的Vue中,它仍然指向旧变量
- python - 列表的 Python 格式(单行)
- google-apps-script - 谷歌脚本格式日期创建不可能的日期
- javascript - 如何创建具有 2 个键和 1 个值的对象?
- python - 是否有相当于 `pip install -t` 的`conda`
- ffmpeg - ffmpeg drawtext 支持藏文字体
- sql - SQL 在搜索下方查找结果 1 字段
- shell - 如何拆分路径以获取shell脚本中的目录
- javascript - 如何使用脚本仅隐藏一半图像?
- java - Java 多线程应用程序 - 在 Hive 上间歇性地获取“错误文件描述符”异常