A schema based spa (single page application) router built for Svelte3.
The package exports 4 constants:
Hash mode is better suited por static single page application, since it doesn't require any server special configuration. Users can share the link or refresh the page and the router won't get lost.
Look at the example folder in the project github for a real life example including all the features provided
# you can use npm
npm install --save @n0n3br/svelte-hash-spa-router
# or yarn
yarn add @n0n3br/svelte-hash-spa-router
<!-- App.svelte -->
<script>
import { Router, Link } from '@n0n3br/svelte-hash-spa-router';
import Home from './Home.svelte';
import About from './About.svelte';
import Redirect from './Redirect.svelte';
const routes = [
{ name: 'home', path: '/', component: Home},
{ name: 'about', path: '/about', component: About},
// redirect route won't render because of the beforeRouteEnter guard
{ name: 'redirec', path: '/redirect', component: Redirect, beforeRouteEnter: navigate => navigate('home', {}, {}) }
]
const linkConfig = {
activeClass: 'is-active', // class applied when the link is active
class: 'nav-link' // class applied to the a element of the link
}
</script>
<main>
<ul>
<li><Link to='home' config={linkConfig}>Home</Link></li>
<li><Link to='about' config={linkConfig}>About</Link></li>
<li><Link to='redirec' config={linkConfig}>Redirect</Link></li>
</ul>
<Router {routes} />
</main>
Provides the router container that will choose wich component to render based on the browser url.
Property | Required | Default Value | Description |
---|---|---|---|
routes |
✔ | [ ] | An array that describes the routes. Each route is composed of a name, a path (/main for example), a component and an optional beforeRouteEnter guard |
The router will export route (the active route path and name), params (url params with name and value), and query (query params with name and value) to the rendered route. All you need to do is put a export let route, params, query
in the script portion of your component.
Each individual route in the routes array of the Router component
Property | Required | Default Value | Description |
---|---|---|---|
name |
✔ | '' | A string to name the route. The name can be used in the Link component or in the navigate function |
path |
✔ | '' | A string that describe the path of the route. To insert parameters passed by the url in the path, prepend their name with : (/post/:id for example). You can have as many parameters as you want in the path |
component |
✔ | null | The Svelte component that will be rendered when the route path matches the navigator url |
beforeRouteEnter |
true | A function that can be used as a guard to prevent the route render. The function receives the navigate method, making possible a redirection to another route if needed. If the function returns false, the route will not be rendered. If the function returns the redirect method, the router will render the passed route (for example redirect('main') or redirec('')). Any other returned value will make the route render. |
Provides an easy way to render a link to a route. The rendered element will be an A.
Property | Required | Default Value | Description |
---|---|---|---|
to |
✔ | '' | The name of the route that will be rendered when the link is clicked. |
params |
{ } | An object with the URL params that should be applied to the route path. The name of the parameters must match the route definition | |
query |
{ } | An object with the query params that should be applied to the route path. | |
config |
{ class: '', activeClass: 'active'} | An object with class and activeClass string parameteres. | |
match |
null | A regular expression that can be used along with route path and name to determine if the link should have the active class |
A method that can be used to do programatic navigation. It can have 3 parameters: to, params and query. Examples:
Property | Required | Default Value | Description |
---|---|---|---|
to |
✔ | '' | The name of the route that will be rendered when the link is clicked. |
params |
{ } | An object with the URL params that should be applied to the route path. The name of the parameters must match the route definition | |
query |
{ } | An object with the query params that should be applied to the route path. |
A subscribable Svelte Store without set and update methods. It provides access to 4 parameters:
You can use the store to have access to the router information in any component, including the ones that are not children of the router.
You can subscribe to it's updates like any other Svelte store:
import { store } from "@n0n3br/svelte-hash-spa-router";
let routes, route, params, query;
store.subscribe((state) => {
({ router, route, params, query } = state);
});