In a typical Svelte project, what is the role of .svelte files?
Think about how Svelte components combine structure, style, and behavior.
Svelte uses .svelte files to bundle HTML markup, CSS styles scoped to the component, and JavaScript logic together. This makes each file a self-contained UI piece.
In a Svelte project, which file is the best place to put styles that apply to the entire app?
Global styles affect the whole app and are usually imported once at the top level.
Global CSS is typically placed in a dedicated CSS file like app.css inside src and imported in the root layout or main entry file to apply styles app-wide.
Given this import statement in a Svelte file:
import Button from '../components/Button.svelte';
What is the most likely cause if this import fails with a module not found error?
Check the folder structure and relative path carefully.
Import errors usually happen when the path does not match the actual location of the file. Svelte supports relative imports with extensions, so the path must be correct.
src folder in a Svelte project?Why do most Svelte projects have a src folder, and what should it contain?
Think about where you write your app's code before building.
The src folder is the main place to write and organize your app's source code including components, styles, and scripts before the build process.
Consider a Svelte project with this structure:
src/
components/
Counter.svelte
routes/
+page.svelteThe Counter.svelte component has a button that increments a count state. The +page.svelte imports and uses Counter. What happens when the button is clicked?
<!-- Counter.svelte --> <script> let count = 0; function increment() { count += 1; } </script> <button on:click={increment}>Clicked {count} times</button> <!-- +page.svelte --> <script> import Counter from '../components/Counter.svelte'; </script> <main> <h1>Welcome</h1> <Counter /> </main>
Remember how Svelte components manage their own state and update the UI reactively.
The Counter component has a local count variable that increments on button clicks. Svelte updates the button text automatically to show the new count.