简体   繁体   English

laravel livewire组件中的JS点击事件监听器?

[英]JS click event listener in laravel livewire component?

I'm trying to add some JS to add a click event to an element within a livewire component.我正在尝试添加一些 JS 以将点击事件添加到 livewire 组件中的元素。 The click event works as expected on first load, however as soon as I run a wire:click, the JS click events no longer work?单击事件在第一次加载时按预期工作,但是一旦我运行 wire:click,JS 单击事件就不再工作了吗?

I can see that livewire is removing / updating the dom elements when I click the wire:click element so unsure whether this may have something to do with it.当我单击 wire:click 元素时,我可以看到 livewire 正在删除/更新 dom 元素,因此不确定这是否与它有关。

What am I doing wrong?我究竟做错了什么? How should I be registering click events to livewire elements in JS and ensure they always work?我应该如何将点击事件注册到 JS 中的 livewire 元素并确保它们始终有效?

My javascript:我的 javascript:

document.addEventListener('livewire:load', function () {

    let pb_responsive_button = [...document.querySelectorAll('.js-pb-responsive')];

    pb_responsive_button.map(function (btn) {
        btn.addEventListener("click", function () {
            console.log('click');
        });
    });

});

livewire:活线:

<button wire:click="addComponentActive(true)">
   <span class="font-bold text-sm">Add Page Block</span>
</button>

public function addComponentActive(bool $bool)
{
    $this->add_component_active = $bool;
}

my view:我的看法:

@if ($add_component_active === true)
    <section class="bg-white shadow-lg rounded-sm mb-8 w-full h-[calc(100%-64px)] absolute z-50">
        .....
    </section>
@endif
    <section class="px-4 sm:px-6 lg:px-8 py-8 w-full max-w-9xl mx-auto">
        <div class="border border-gray-200 rounded">
            <div class="bg-gray-200 p-5 flex justify-between">
                <div class="bg-white border border-gray-200 rounded p-2">
                    <span class="font-bold text-sm">Homepage</span>
                </div>
                <div class="bg-blue-600 text-white border border-gray-200 rounded p-2">
                    <button wire:click="addComponentActive(true)">
                        <span class="font-bold text-sm">Add Page Block</span>
                    </button>
                </div>
                <div class="flex justify-between items-center gap-2">
                    <div class="js-pb-responsive js-pb-mobile bg-white border border-gray-200 rounded p-2">M</div>
                    <div class="js-pb-responsive js-pb-tablet bg-white border border-gray-200 rounded p-2">T</div>
                    <div class="js-pb-responsive js-pb-desktop bg-white border border-gray-200 rounded p-2">D</div>
                </div>
            </div>
        </div>
    </section>

UPDATE:更新:

I've updated it to show and hide the blocks rather than display it in a conditional if statement which ensures the click events still work, since the element still exists on page.我更新了它以显示和隐藏块而不是在条件 if 语句中显示它,这确保了点击事件仍然有效,因为该元素仍然存在于页面上。

<section class="{{$add_component_active === true ? 'block' : 'hidden'}}"> ... </section>

Surely there is a better way to handle this.当然有更好的方法来处理这个问题。 I notice I have a livewire:load eventListener?我注意到我有一个 livewire:load eventListener? Is there another event I should be listening to when the livewire view is reloaded to reinitise the JS events?当重新加载 livewire 视图以重新启动 JS 事件时,我应该听另一个事件吗?

Any help would be much appreciated.任何帮助将非常感激。

Livewire works hand-in-hand with Alpine.js. Livewire 与 Alpine.js 携手合作。 You should focus on Alpine if you want strictly front-end functionality https://alpinejs.dev/directives/on如果你想要严格的前端功能,你应该专注于 Alpine https://alpinejs.dev/directives/on

<button @click="alert('Hello World!')">Say Hi</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM