Skip to main content

Bất cứ thứ gì được xuất từ khối mã nguồn context="module" sẽ trở thành một xuất từ mô-đun đó. Hãy xuất một hàm stopAll:

AudioPlayer.svelte
<script context="module">
	let current;

	export function stopAll() {
		current?.pause();
	}
</script>

Bây giờ chúng ta có thể nhập stopAll trong App.svelte...

App.svelte
<script>
	import AudioPlayer, { stopAll } from './AudioPlayer.svelte';
	import { tracks } from './tracks.js';
</script>

...và sử dụng nó trong một xử lý sự kiện:

App.svelte
<div class="centered">
	{#each tracks as track}
		<AudioPlayer {...track} />
	{/each}

	<button on:click={stopAll}>
		stop all
	</button>
</div>

Bạn không thể có một xuất mặc định (default export), vì component xuất mặc định.

Tiếp theo: Miscellaneous

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<script>
	import AudioPlayer from './AudioPlayer.svelte';
	import { tracks } from './tracks.js';
</script>
 
<div class="centered">
	{#each tracks as track}
		<AudioPlayer {...track} />
	{/each}
</div>
 
<style>
	.centered {
		display: flex;
		flex-direction: column;
		height: 100%;
		justify-content: center;
		gap: 0.5em;
		max-width: 40em;
		margin: 0 auto;
	}
</style>
initialising