【发布时间】:2021-03-06 11:36:26
【问题描述】:
刚刚试用 Livewire,但我一直卡在路上。我到了需要传递 dom 元素值的阶段,但不确定执行此操作的最佳实践,是否甚至可以通过 livewire?
查看:
<div>
<textarea class="form-control mb-3" wire:keydown.enter="addComment('Guest', 'This textareas value here')"></textarea>
<hr>
@foreach ($comments as $comment)
<div class="card mt-4" style="background-color: deeppink">
<div class="card-body">
<h5 class="card-title">{{ $comment['author'] }}</h5>
<p class="card-text">{{ $comment['body'] }}</p>
</div>
</div>
@endforeach
</div>
组件:
<?php
namespace App\Http\Livewire;
use Livewire\Component;
class Comments extends Component
{
public $comments = [
[
'author' => 'John Doe',
'body' => 'I really like short walks through the park near my house.',
],
[
'author' => 'Samantha Hox',
'body' => 'Did you know, that Lions are actually bigger than the size you imagined. They are beautiful creatures so they are.',
],
];
public function render()
{
return view('livewire.comments');
}
public function addComment($author, $body) {
array_unshift($this->comments, [
'author' => $author,
'body' => $body
]);
}
}
【问题讨论】: