sanket kheni
sanket kheni

Reputation: 1628

Routing in Svelte ? (Navigate between pages)

I am new to svelte. I want to know is there any inbuilt functionality to navigate between pages in svelte or do I have to use a third-party library like (As react-router for react.js)?

Upvotes: 3

Views: 4624

Answers (2)

user486543
user486543

Reputation: 113

goto function https://kit.svelte.dev/docs/modules#$app-navigation

Passing in your route to the goto('route') function should work.

<script>
  import { goto } from '$app/navigation';

  function navigate() {
    goto('/another-route');
  }
</script>

<main>
  <button on:click={navigate}>
    Go to Another Route
  </button>
</main>

Upvotes: 3

Stephane Vanraes
Stephane Vanraes

Reputation: 16411

There is no built in router for Svelte. But there are several other options.

For single-page-applications, there are svelte-router, tinro, and others. For multi page, the three big ones currently are Routify, ElderJS and SvelteKit.

Routify brings you routing and some other stuff (haven't used it often myself)

ElderJS is excellent for making very large static sites (1000+ pages)

SvelteKit is made by the Svelte team itself and is worth looking at if you want server side rendering, it is comparable to NextJS for React.

https://www.npmjs.com/package/svelte-routing

https://www.npmjs.com/package/tinro

https://www.routify.dev/

https://github.com/Elderjs/elderjs

https://kit.svelte.dev/

Upvotes: 4

Related Questions