2.0.11 • Published 2 years ago

@fraserdarwent/svelte-router v2.0.11

Weekly downloads
28
License
ISC
Repository
github
Last release
2 years ago

svelte-router

Installation

yarn add @fraserdarwent/svelte-router

Usage

<script>
  import Router from '@fraserdarwent/svelte-router';
  import HomeComponent from './home-component.svelte';
  import AllPicturesComponent from './all-pictures-component.svelte';
  import AllDogPicturesComponent from './all-dog-pictures-component.svelte';
  import AllCatPicturesComponent from './all-cat-pictures-component.svelte';
  import SpecificCatPicturesComponent from './specific-cat-pictures-component.svelte';

  const routes = [
    {
      // Effective route is /
      path: '/',
      component: HomeComponent,
    },
    {
      // Effective route is /pictures
      path: '/pictures',
      component: AllPicturesComponent,
      routes: [
        {
          // Effective route is /pictures/dogs
          path: '/dogs',
          component: AllDogPicturesComponent,
        },
        {
          // Effective route is /pictures/cats
          path: '/cats',
          component: AllCatPicturesComponent,
          routes: [
            {
              // Effective route is /pictures/cats/*
              path: '/*',
              component: SpecificCatPicturesComponent,
            },
          ],
        },
      ],
    },
  ];
</script>

<div id="app">
  <Router {routes} />
</div>

Navigating

Use the route method

<script>
  import {route} from '@fraserdarwent/svelte-router';
</script>
<button on:click={()=>{route('/videos')}}>Videos</button>

Setting A Fallback

A fallback component can be specified for when no route is matched

<script>
  import Router from '@fraserdarwent/svelte-router';
  import FourOFour from './404.svelte';

  const routes = [];
</script>

<div id="app">
  <Router {routes} fallback="{FourOFour}" />
</div>

Routing Methods

Path

Router defaults to path routing method

To Use Hash Routing Method

Hash

<Router {routes} method="hash" />
2.0.11

2 years ago

2.0.9

3 years ago

2.0.10

3 years ago

2.0.8

3 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago