【发布时间】:2021-01-29 22:10:16
【问题描述】:
使用 Laravel Livewire,我有一个父母和一个(重复的)孩子。子刀片通过wire:click="childMethod()" 调用了childMethod()。
问题是 parent->childMethod() 被称为为什么我想要 child->childMethod() 被调用。
父组件
class StatementsTable extends Component // parent
{
public function render()
{
return view('livewire.statements-table', [
'statements' => Statement::limit(10)->get()
]);
}
}
父statements-table.blade
<table class="table">
@foreach($statements as $statement)
@livewire('statement-line', ['statement' => $statement], key($statement->id))
@endforeach
</table>
子组件:
class StatementLine extends Component
{
public $statement;
public $calls = 0;
public function childMethod()
{
$this->calls += 1;
}
public function mount($statement): void
{
$this->statement = $statement;
}
public function render()
{
return view('livewire.statement-line');
}
}
孩子statement-line.blade
{{-- dd(get_defined_vars()) --}}
<tr>
<td>{{$statement->name}}</td>
<td>{{$statement->date}}</td>
<td>{{$calls}}</td>
<td><button wire:click="childMethod">Plus</button></td>
</tr>
我为什么会得到
Livewire\Exceptions\MethodNotFoundException
Unable to call component method. Public method [childMethod] not found on component: [statements-table]
【问题讨论】:
-
代码太多,无法回答
-
@PaulH 我也遇到了这个问题。你找到解决办法了吗?
标签: php laravel laravel-livewire