【发布时间】:2021-12-05 13:18:22
【问题描述】:
我有两个加载 livewire 组件的视图
<x-layouts.formularies>
<livewire:formularies.edit.display-section :section="$section->id" :formulary="$formulary->id" />
</x-layouts.formularies>
这个组件(display-section)在循环中调用一个子组件...
<div class="letter mx-auto" id="section-holder">
<div class="paper-title d-flex justify-content-between">
@foreach($questionnaire->getData( 'logos', [] ) as $logo)
<img src="{{ asset($logo[ 'path' ]) }}" class="img-fluid h-100" />
@endforeach
</div>
<div class="section-questions">
@foreach($section->questions as $question)
<livewire:formularies.edit.display-row :question="$question->id"
:formulary="$formulary->id" :wire:key="$question->id" />
<hr />
@endforeach
</div>
</div>
现在,这是 display-row 组件:
<div class="card-body text-center p-1">
<div class="btn-group m-1" role="group" aria-label="question-{{ $question->id }}">
@foreach($question->answers as $answer)
<input type="radio"
wire:model="answer"
:wire:key="$question->id . '-' . $answer->id"
wire:click="setAnswer"
class="btn-check"
value="{{ $answer->id }}"
name="question-{{ $question->id }}"
id="question-{{ $question->id }}-{{ $answer->id }}"
autocomplete="off" />
<label class="btn btn-outline-teal text-dark mb-0"
for="question-{{ $question->id }}-{{ $answer->id }}">{{ $answer->statement }}</label>
@endforeach
</div>
</div>
当用户从单选按钮中选择一个选项时,它会执行一些数据库操作并触发应该由父组件 (display-section) 监听的事件:
public function setAnswer()
{
$this->emit( 'formularyUpdated', $this->formulary->id );
FormularyUpdated::dispatch( $this->formulary, $this->question, $this->answer );
dd( 'I should not get here if there is a dd before' );
}
如您所见,此函数会发出一个带有公式 ID 的 formularyUpdated 事件。在 DisplaySection 我有这个:
protected $listeners = [
'sectionAdvanced',
'formularyUpdated',
];
/**
*
*/
public function formularyUpdated( Formulary $formulary )
{
dd( 'This dd should stop the execution' );
}
由于某种原因,这总是以 "I should not get here if there is a dd before" 结尾,那么,我应该改变什么?为什么我的听众不听或不被解雇?我尝试了不同的方法,但没有运气。
【问题讨论】:
标签: laravel events listener laravel-8 laravel-livewire