svelte-guard-history-router
svelte guarded history router
usage
import { Router, route, Guard } from 'svelte-guard-history-router';
let session = undefined;
class SessionGuard extends Guard {
async enter(transition) {
if(!session) {
transition.redirect('/login');
}
}
}
export const router = new Router(
[
route("*", Home),
route("/about", About),
route("/login", Login),
route("/article", sessionGuard, Articles),
route("/article/:article", sessionGuard, Article),
route("/category", sessionGuard, Categories),
route("/category/:category", sessionGuard, Category)
],
"/base"
);
run tests
export BROWSER=safari|chrome|...
npm|yarn test
API
Table of Contents
Key
Type: Object
Properties
RouterState
Type: Object
Properties
Router
key subscriptions:
const aKey = router.keys.aKey;
$aKey
Parameters
routes
Array<Route> (optional, default []
)base
string url (optional, default ""
)
Properties
push
Leave current route and enter route for given path
Parameters
subscribe
Router subscription
Value changes are fired when the route (or the target component changes)
Parameters
updateActive
Parameters
Transition
Parameters
Properties
start
start the transition
- find matching target route
- leave old route
- set params
- set current route
- enter new route
end
cleanup transition
redirect
Halt current transition and got to another route
Parameters
continue
Continue a redirected route to its original destination
rollback
Bring back the router into the state before the transition has started
Parameters
Route
Parameters
path
stringcomponent
SvelteComponent target to show
Properties
enter
Enter the route from a former one.
Calls guard enter on all guards present in our gurad but absent in the former one
Parameters
leave
Leave the route to a new one.
Calls quard leave on all our guards which are not in the new route
Parameters
GuardedRoute
Extends Route
Parameters
path
stringcomponent
SvelteComponent target to showguard
Guard
Properties
enter
Enter the route from a former one.
Calls guard enter on all guards present in our gurad but absent in the former one
Parameters
leave
Leave the route to a new one.
Calls quard leave on all our guards which are not in the new route
Parameters
route
Helper function to create routes with optional guards
Parameters
Guard
Enforces conditions of routes
Like presents of values in the context
attach
Called when guard is attached to a route (one time)
Parameters
enter
Called while entering a route (current outlet is not yet set)
Parameters
leave
Called before leaving a route
Parameters
sequenceGuard
execute guards in a sequence
Parameters
parallelGuard
execute guards in a parallel
Parameters
install
With npm do:
npm svelte-guard-history-router
license
BSD-2-Clause