Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
how to dispatch param using x-init in Alpinejs

My Alpinejs component code is in an EventListener and I wish to use the magic $dispatch to pass a title string from outside the component. I can only achieve this using an onclick event but really need to do this on page load.

This is the code outside of the component used to pass in the title using @click. This works but it needs to be set when the page is loaded (in Alpinejs I believe this is x-init)


<div x-data>
  <button 
    @click="$dispatch('myparams', { title: 'The title' })"
    type="button" 
  >
    More info
  </button>
</div>

Using x-init does not work with the below

<div 
  x-data 
  x-init="() => $dispatch('myparams', {'title': 'Initialised'})"
// x-init="$dispatch('myparams', {'title': 'Initialised'})"
>
</div>

Here is my component with what I've tried to receive data from outside

<section 
  x-data="mypage"
  @myparams.window="title = $event.detail.title"
>
  <h2 x-text="title"></h2>
</section>

<script>
document.addEventListener('alpine:init', () => {
    Alpine.data('mypage', () => ({
      title: '',
      init() {
        @myparams.window="title = $event.detail.title"
        console.log('I will get evaluated when initializing this component.')
      },
    }))
})
</script>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The reason of this behavior is that you dispatch the myparams event before the initialization of listener for myparams event. So the first time no one is listening.

The future-proof solution is to use $nextTick so the dispatch fires after DOM updates.

<div x-data="initMyparams"></div>

<section x-data="mypage" @myparams.window="title = $event.detail.title">
  <h2 x-text="title"></h2>
</section>

Where we have a new initMyparams component using $nextTick:

<script>
document.addEventListener('alpine:init', () => {
    Alpine.data('mypage', () => ({
        title: '',
        init() {
            console.log('I will get evaluated when initializing this component.')
        },
    })),

    Alpine.data('initMyparams', () => ({
        init() {
            this.$nextTick(() => this.$dispatch('myparams', { title: 'Initialised' }))
        },
    }))
})
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

Managed to figure it out. Basically All I needed to do was include the component on the page BEFORE the onload (x-init) event.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda