简体   繁体   English

为什么我的事件监听器在第二个事件之后没有触发?

[英]Why is my Event Listener not Firing After the Second Event?

I'm building a photo upload form with Livewire, Alpine, and FilePond.我正在使用 Livewire、Alpine 和 FilePond 构建照片上传表单。 After the photos have finished processing I want to show a "Save" button to persist the files.照片完成处理后,我想显示一个“保存”按钮来保存文件。

I'm using Alpine to handle the show/hide.我正在使用 Alpine 来处理显示/隐藏。 Both event listeners are working correctly when separated, but I just can't seem to get them to work together.两个事件侦听器在分开时都可以正常工作,但我似乎无法让它们一起工作。 The below code is not throwing errors, but it's also not showing the buttons inside the showSaveButtons div.下面的代码没有抛出错误,但也没有显示 showSaveButtons div 中的按钮。

What am I doing wrong?我究竟做错了什么?

<div x-data="showSaveButtons">
    <div x-show="open">
        Buttons Go Here
    </div>
</div>
<script>
    document.addEventListener('alpine:init', () => {
        Alpine.data('showSaveButtons', () => ({
            open: false,
        }));
        document.addEventListener("FilePond:processfiles", () => {
            Alpine.data('showSaveButtons', () => ({
                open: true,
            }));
        });
    })
</script>

This is because you're creating a second Alpine.data object, instead of editing the existing one.这是因为您正在创建第二个Alpine.data object,而不是编辑现有的。

<script>
    document.addEventListener('alpine:init', () => {
        Alpine.data('showSaveButtons', () => ({
            open: false,

            init() {
                // So we can access "this" inside the listener
                let context = this;

                document.addEventListener("FilePond:processfiles", () => {
                    context.open = true;
                }); 
            }
        });
    });
</script>

If you're curious about the init function, check the docs如果您对init function 感到好奇,请查看文档

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

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