Overview
Svelte Router is a component for Svelte v3.0.0+ that allows for easy routing within a Svelte application. It provides a set of methods and an API for navigating to different routes, as well as a Link component for easy navigation between routes.
Features
- Router Component: The main component of Svelte Router that handles routing within the Svelte application.
- push(): A method that allows for navigating to a specified path.
- replace(): A method that allows for navigating to a specified path and replacing the current page in the browser’s history.
- go(): A method that allows for navigating to a specified index in the browser’s history.
- goBack(): A method that allows for navigating to the previous page in the browser’s history.
- goForward(): A method that allows for navigating to the next page in the browser’s history.
- listen(): A method that allows for registering a callback function to be called whenever the route changes.
- Link Component: A component that provides easy navigation to a specified path using an anchor tag. It also allows for customization with optional props such as “replace”, “className”, and “activeClassName”.
Installation
To use Svelte Router in your Svelte project, you can install it via npm or yarn:
Using npm:
npm install svelte-router
Using yarn:
Summary
Svelte Router is a powerful routing solution for Svelte applications. It provides a Router component that handles routing and a set of methods and an API for navigating between routes. Additionally, it offers a Link component for easy navigation using anchor tags. With its intuitive API and features, Svelte Router simplifies the implementation of routing within Svelte applications.