php 你好,我在Laravel livewire listner面临一个问题不工作

z4iuyo4d  于 2023-09-29  发布在  PHP
关注(0)|答案(1)|浏览(97)
namespace App\Http\Livewire\Survey;

use Livewire\Component;
use \App\Models\Section as SectionModel;

class Section extends Component
{
    public $questionnaireId;
    public $section;
    public $currentSection = 0;
    public $listeners = ['nextSection'];

    public function nextSection()
    {
        $this->currentSection++;
        $this->currentSection = $this->getSections($this->currentSection);
    }

    public function getSections($offset = 0)
    {
        $this->section = SectionModel::where('questionnaire_id', $this->questionnaireId)->offset($offset)->first();
    }

    public function mount($questionnaireId)
    {
        $this->questionnaireId = $questionnaireId;
        $this->getSections();
    }

    public function getSectionsProperty()
    {
        return $this->section;
    }
}

这是我的componentOne.php

public function triggerNextSection()
{
    $this->emit('nextSection');
}

这是component-one.blade.php

<button wire:click="triggerNextSection"
        class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"> Continue
</button>

当单击继续按钮时,$currentSection值增加1。
与我使用此按钮并在componentTwo中发出此事件的方式相同,这里不递增$currentSection的值。
有没有人可以帮助我如何增加这个变量的值?
我尝试发出一个带有传递参数的事件

b4lqfgs4

b4lqfgs41#

试着这样做:

protected $listeners = ['nextSection','mynextSection',];

public function mynextSection()
{
    $this->currentSection++;
    $this->currentSection = $this->getSections($this->currentSection);
}

相关问题