Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #330
    jacob_smith
    Participant

    I have a livewire component with a function as follow:

    <?php

    namespace App\Http\Livewire\Post;

    use Livewire\Component;

    class PostCreate extends Component
    {

    public function addPost($post)
    {
    dd($post);
    }

    public function render()
    {
    return view(‘livewire.post0create’);
    }
    }

    How do I call addPost function inside alpine js from the corresponding livevire view component

    The livewire view component looks like this:

    <button x-on:click=”addPost(post)”></button>

    <script>
    JS code here
    </script>

    • This topic was modified 1 year, 2 months ago by jacob_smith.
    • This topic was modified 1 year, 2 months ago by jacob_smith.
    #333

    You can call a function in a livewire class component from the alpine js of the livewire view component using @this.call helper as follow:

    — Livewire view markup —

    <button x-on:click=”addPost(post)”></button>

    <script>
    addPost(post) {
    $this.call(‘addPost’, post)
    }
    </script>

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.