簡體   English   中英

如何將數據從 Livewire 組件發送到 controller?

[英]How to send data from Livewire component to a controller?

我有一個非常基本的 Livewire 組件。 我知道,有一個wire:click ,但我非常簡化它,手工給你一些東西。 在我的真實應用程序中,有一個日歷組件,當您單擊一個事件時,它會觸發emit()並將數據從 ui 發送到 Livewire 組件,並在其中進行修改。

我的問題是:如何將這些數據從 Livewire 組件發布到foo.store路由?

應用程序/Http/Livewire/Foo.php:

class Foo extends Component
{
    public $foo;

    protected $listeners = [
         'store' => 'store'
    ];

    public function render()
    {
         return view('livewire.foo');
    }

    public function store($data)
    {
         $payload = $this->do_complex_math_on_data($data);
         // ❓ post payload to FooController's store() function - HOW?
    }

    private function do_complex_math_on_data($data)
    {
         return 1+1;
    }
}

資源/視圖/livewire/foo.blade.php:

<div>
   <button>Click me!</button>
</div>

<script>
    document.addEventListener('livewire:load', function() {
        document.querySelector('button').addEventListener('click' () => {
            Livewire.emit('store', data.coming.from.ui);
        });    
    });
</script>

From your prospect of the controller, as Peppermintology stated, there are two types of controller, one is a standard Laravel controller and the second is a Livewire component controller, from there you have to understand that the livewire controller lifecycle is detached from Laravel. it作為瀑布工作,您可以將數據從 Laravel controller 注入到 Livewire 組件,但是從 Livewire 到 Laravel 標准組件您不能,盡管您可以在 livewire 生態系統之間傳遞數據,例如從父組件到子組件,反之亦然

像下面一樣

$this->emitUp('postAdded');

或者

<button wire:click="$emitUp('postAdded')">

您可以在 livewire嵌套組件中閱讀有關嵌套組件的更多信息,以及有關事件事件的信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM