You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import { useTinyRouter } from 'svelte-tiny-router';
57
102
const router = useTinyRouter();
58
103
104
+
// Access the entire query object
105
+
console.log("Current query:", router.query);
106
+
59
107
// Check if the "foo" query parameter exists (i.e /myroute?foo=bar) and log it
60
108
if (router.hasQueryParam('foo')) {
61
109
console.log("Value of foo:", router.getQueryParam('foo'));
62
110
router.removeQueryParams(["foo"]);
63
111
}
112
+
113
+
// Get a specific query parameter
114
+
const searchTerm = router.getQueryParam('q');
115
+
console.log("Search term:", searchTerm);
64
116
</script>
65
117
```
118
+
119
+
## Navigation Guards (`beforeEach`)
120
+
121
+
You can define navigation guards using the `beforeEach` prop on the `<Router>` component. These guards are functions that are executed before each navigation. They can be used to cancel navigation, redirect to a different route, or perform asynchronous tasks like authentication checks.
122
+
123
+
```svelte
124
+
<!-- App.svelte (with navigation guards) -->
125
+
<script>
126
+
import { Router, Route } from 'svelte-tiny-router';
127
+
import Home from './Home.svelte';
128
+
import AdminDashboard from './AdminDashboard.svelte';
129
+
import Login from './Login.svelte';
130
+
131
+
// Example authentication check function
132
+
function isAuthenticated() {
133
+
// Replace with your actual auth logic (e.g., check token in localStorage)
A navigation guard function receives an object with the following properties:
169
+
-`to`: An object representing the target route (`{ path: string, params: Record<string, string>, query: Record<string, string> }`).
170
+
-`from`: An object representing the current route, or `null` if this is the initial navigation (`{ path: string, params: Record<string, string>, query: Record<string, string> } | null`).
171
+
-`next`: A function that must be called to resolve the hook.
172
+
-`next()`: Proceed to the next hook in the pipeline, or to the navigation if no more hooks are left.
173
+
-`next(false)`: Cancel the current navigation.
174
+
-`next('/path')` or `next({ path: '/path', replace: true })`: Redirect to a different location. The current navigation is cancelled, and a new one is initiated.
175
+
176
+
## Nested Routing
177
+
178
+
The library supports nested routing, particularly useful with wildcard routes (`/*`). When a wildcard route matches, it automatically sets up a `NestedRouterProvider` context for its children `<Route>` components. These children routes then match paths relative to the parent wildcard's matched segment.
Navigating to `/app/settings` will first match the `/app/*` route. The `NestedRouterProvider` within `/app/*` then makes `/settings` match relative to `/app`.
190
+
191
+
Alternatively, you can render a separate component that contains its own `<Router>` instance for nested routes. This component will receive the matched parameters from the parent route.
192
+
193
+
```svelte
194
+
<!-- App.svelte -->
195
+
<script>
196
+
import { Router, Route } from 'svelte-tiny-router';
197
+
import Home from './Home.svelte';
198
+
import About from './About.svelte';
199
+
import User from './User.svelte';
200
+
import DashboardRouter from './DashboardRouter.svelte'; // Component containing nested routes
201
+
</script>
202
+
203
+
<Router>
204
+
<Route path="/" component={Home} />
205
+
<Route path="/about" component={About} />
206
+
<Route path="/user/:id" component={User} />
207
+
208
+
<!-- Wildcard route rendering a component that contains a nested router -->
This library now includes comprehensive TypeScript definitions, providing improved type checking and autocompletion for users of TypeScript or JavaScript with JSDoc. Key types include `RouterContext`, `RouteInfo`, `NavigationGuard`, and `NextFunction`.
0 commit comments