svelte-router-file Svelte Themes

Svelte Router File

Extension of @svelte-router/core that provides a file-based router

What this is

This is a plugin package for @svelte-router/core, which should be installed. It follows the routing format established by sv-router.

Installation

npm i svelte-router-file

Usage

  1. Initialize the library
  2. Mount the FileRouter

    Initializing the library

    Initialize the library in main.js or similar, before any FileRouter is mounted:
import { init } from "svelte-router-file";

/* basePath is set to ./routes if not specified */
init({ basePath: "./myroutes" });

Initializing this library means that you should not call the usual init() function of @svelte-router/core.

// BAD:
import { init } from "@svelte-router/core";

init();

Initializing the FileRouter

In the root component, like App.svelte or similar:

<script lang="ts">
    import { FileRouter } from "svelte-router-file";

    let files = import.meta.glob("routes/**/*.svelte", { eager: true });
</script>

<FileRouter {files} />

Top categories

Loading Svelte Themes