Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

204
Vistas
How to call function from another component to another component

Now I'm confused how to fire function from component to control something in another component. Can anyone help me on this?

Structure:
App.svelte
L checklist.svelte
  L button <-- this button want to call function in stage component
L stage.svelte
checklist.svelte:
<button on:click="handleMove" />
stage.svelte:
export function fly(x, y) {
   $map.flyto(x, y);
}

I have a button in checklist component that want to activate function that in stage component.

Stage component is a map that have x y position and function that move things inside this component.

How can I call function in stage component from the outside (checklist components)?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You have a couple of options:

first and the most flexible one is to create a writable store to use it anywhere

second it to use context

third is to declare it in App.svelte and bind it to both children

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could do the following:

  1. Dispatch a custom checklist-click event from Checklist.
  2. In App, listen to this click event and call fly on Stage.
<!-- App.svelte -->
<script>
    import Checklist from './Checklist.svelte';
    import Stage from './Stage.svelte';
    
    let stage;
    
    function handleClick({ detail }) {
        // these come from the second argument to dispatch in Checklist.svelte
        const { x, y } = detail;
        stage.fly(x, y);
    }
</script>

<h1>App</h1>

<!-- Listen to the event fired with dispatch('checklist-click') -->
<Checklist on:checklist-click={handleClick}></Checklist>

<!-- Store a reference to this component instance so that we can call `fly` on it -->
<Stage bind:this={stage}></Stage>

<!-- Checklist.svelte -->
<script>
    import { createEventDispatcher } from 'svelte';
    const dispatch = createEventDispatcher();
    
    function handleMove() {
        dispatch('checklist-click', {x: 5, y: 10});
    }
</script>

<h2>Checklist</h2>

<button on:click={handleMove}>
    Move
</button>

<!-- Stage.svelte -->
<script>
    export function fly(x, y) {
        console.log(`fly called from stage with x: ${x}, y: ${y}`);
    }
</script>


<h2>Stage</h2>

For more on dispatching component events, see the Svelte tutorial.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda