Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Svelte 3 Up and Running

You're reading from   Svelte 3 Up and Running A fast-paced introductory guide to building high-performance web applications with SvelteJS

Arrow left icon
Product type Paperback
Published in Aug 2020
Publisher Packt
ISBN-13 9781839213625
Length 168 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Alessandro Segala Alessandro Segala
Author Profile Icon Alessandro Segala
Alessandro Segala
Arrow right icon
View More author details
Toc

Routing with hash-based routing

For the first example, we'll implement routing with svelte-spa-router (https://www.npmjs.com/package/svelte-spa-router), which uses hash-based routing.

You can find the final code for this section in the project's GitHub repository in the ch5 folder (for a direct link, go to https://bit.ly/sveltebook-ch5).

To start, install the module with the following command. We're saving it as a dependency and not a devDependency because it will be included in our application's bundle:

npm install --save svelte-spa-router@2

Defining the routes object

Using svelte-spa-router requires defining your routes in a dictionary (object) where the key is the path to match, which can optionally use parameters, and the value is a Svelte component object.

Create a file called src/routes.js that includes our routes dictionary:

src/routes.js

import ViewAdd from './components/ViewAdd.svelte'
import ViewObject from './components...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime