Halo guys welcome back… kali ini kita coba ke materi Component Events ya… jadi kita akan mencoba membuat alert melalui dispatch function..
pertama buat file components/alert/AlertCompEventsComp.svelte
<script>
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
function sayHello() {
dispatch('pesan', {
isi: 'Hello Guysss!'
});
}
</script>
<button on:click={sayHello}>
Click to say hello
</button>
Kemudian kita buat file baru lagi components/AlertCompEvents.svelte
<script>
import AlertCompEventsComp from './alert/AlertCompEventsComp.svelte';
function handleMessage(event) {
alert(event.detail.isi);
}
</script>
<AlertCompEventsComp on:pesan={handleMessage}/>
dan kita import kedalam App.svelte
<script>
import Counter from './components/Counter.svelte'
import Toggle from './components/Toggle.svelte'
import Img from './components/Img.svelte'
import String from './components/String.svelte'
import Statement from './components/Statement.svelte'
import Condition from './components/Condition.svelte';
import Nested from './components/Nested.svelte';
import Info from './components/Info.svelte';
import IfLogin from './components/IfLogin.svelte'
import Comparison from './components/Comparison.svelte'
import EachYoutube from './components/EachYoutube.svelte'
import KeyEachThing from './components/KeyEachThing.svelte'
import GenerateNumber from './components/GenerateNumber.svelte';
import MouseMove from './components/MouseMove.svelte'
import MouseMoveInlineHandler from './components/MouseMoveInlineHandler.svelte';
import EventModifier from './components/EventModifier.svelte';
import AlertCompEvent from './components/AlertCompEvents.svelte'
export let name;
const pkg = {
name: 'svelte',
version: 3,
speed: 'blazing',
website: 'https://svelte.dev'
};
</script>
<main>
<h1>Hello {name}!</h1>
<p>Visit the <a href="https://svelte.dev/tutorial">Svelte tutorial</a> to learn how to build Svelte apps.</p>
<Counter />
<Toggle />
<Img />
<String />
<Statement />
<Condition />
<Nested answer={42}/>
<Nested/>
<Info {...pkg}/>
<IfLogin />
<Comparison />
<EachYoutube />
<KeyEachThing />
<GenerateNumber />
<MouseMove />
<MouseMoveInlineHandler />
<EventModifier />
<AlertCompEvent />
</main>
<link href="https://fonts.googleapis.com/css?family=Overpass:100,400" rel="stylesheet">
dan resultnya :
jadi kita sudah mengetahui cara untuk passing data pesan untuk masuk kedalam function dari suatu component … okay untuk link githubnya ada disini … cyaaa…