Store-based router for Svelte
The store-based router for Svelte is a routing solution that takes a different approach by considering routing as just another global state. It suggests that History API changes are optional side effects of this state. This router allows for easy manipulation of different parts of the state, such as the path, query, and fragment, individually. It also offers automatic parsing of query and fragment parameters, configurable delay of History changing, conversion of query and fragment string values to JavaScript types, and cleaning of empty values. Additionally, it provides support for handling navigation without reloading the page and works well with server-side rendering.
To install the store-based router for Svelte, follow these steps:
stores.js
file.import { writable } from 'svelte/store';
export const routeStore = writable({});
import { routeStore } from './stores.js';
routeStore.set({ path: '/new-path' });
<script>
import { routeStore } from './stores.js';
let currentRoute;
routeStore.subscribe(route => {
currentRoute = route;
});
</script>
<p>The current route is {currentRoute.path}.</p>
goto
function. Note that the route must be relative to the base path.routeStore.set('/new-path');
// or
routeStore.goto('/new-path');
The store-based router for Svelte offers a unique approach to routing by treating it as just another global state. It provides a range of features such as individual manipulation of routing parts, automatic parsing of parameters, and support for server-side rendering. With easy installation and usage, this router offers a flexible and efficient solution for handling routes in Svelte applications.