The
v
is for Svelte
Built on top of abstract-nested-router, so you can use nested routers, also:
- Advanced parameters can be used, e.g.
/:id<\d+>
β see docs - ARIA-compliant, sets
[aria-current="page"]
on active links - Seamless
<base href="..." />
integration - Conditionals and redirection through props
- Fallback
<Route />
handlers - Hash and URI-based routes
- Support for query-string
- REPL ready!
yrv
will use any base-href found on the current page to rewrite links and routes.
Install yrv
through NPM or Yarn:
<script>
import { Router, Route, Link } from 'yrv';
</script>
<Link href="/">Home</Link>
| <Link href="/World">Hello</Link>
| <Link href="/not/found">NotFound</Link>
<p>
<Router>
<Route exact>Hello World</Route>
<Route fallback>Not found</Route>
<Route exact path="/:name" let:router>Hey {router.params.name}!</Route>
</Router>
</p>
Notice
fallback
routes canβt be placed at the beginning, otherwise further routes will not be mounted. π£
You MUST declare at least, one top-level
Router
to setup the bindings.
This component will hold any given routes as children, path is always derived from parent ones.
Available props:
{path}
β Any segment to derive a fullpath from, default to/
{disabled}
β Boolean; Similar to condition, but for bound props{condition}
β Function; if given, render only if evaluates to true{nofallback}
β If set, non-matched routes will never raise a failure
<Route {key} {path} {props} {exact} {dynamic} {pending} {fallback} {component} {disabled} {condition} {redirect} let:router />
Main container for routing, they can hold any component or children.
Available props:
{key}
β The route identity, not its path; default to random pseudo-hash{path}
β Any segment to derive a fullpath from, default to/
{props}
β Additional properties for rendered component{exact}
β If set, the route will render only if the route exactly matches{dynamic}
β Promise, if set will resolve and use its result as lazy-component{pending}
β String, this value is rendered during the loading of dynamic components{fallback}
β If set, the route will render only if no more routes were matched{component}
β A valid svelte-component to render if the route matches{disabled}
β Boolean; Similar to condition, but for bound props{condition}
β Function; if given, the route will render only if evaluates to true{redirect}
β Alternate redirection location, only if the previous condition was truelet:router
β Injects therouter
context, it also providesfailure
in case of errors
If you omit
exact
, then/x
would match both/
and/x
routes β see docs
In order to navigate, you can use Link
components, or regular links, etc.
All
href
values MUST be absolute, only links starting with/
or#
are allowed.
Available props:
{go}
β History shortcut (see below){href}
β New location, default to/
{open}
β Same behavior as<a target="_blank">
{title}
β HTML title-attribute value{button}
β If set, will use button-tag instead{exact}
β Determine if link should match exactly to be set as active{reload}
β Uselocation.href
instead{replace}
β Usehistory.replaceState()
instead{class}
β Custom class-name for the mounted anchor
The value for
open
can be a string including the window specs, e.g.width=400,height=200
β aon:close
event will be fired once the opened window is closed.
Normal on:click
events are still allowed, so you can use:
<Link on:click={() => location.reload()}>Reload</Link>
Active links will gain the
[aria-current]
attribute, and[disabled]
if they're buttons.
Aditionally, you can setup go
to moving around:
"back"
β String; if given, will invokehistory.back()
"fwd"
β String; if given, will invokehistory.fwd()
n
β Number; if given, it'll be used to invokehistory.go(n)
If navigating through
history
is not possible a normal redirection will run. β
navigateTo(path[, options])
β Change the location, supported options are:reload
β If true, it will usedocument.location.href
insteadreplace
β If true, it will usehistory.replaceState()
insteadparams
β Used to replace:placeholders
from given pathqueryParams
β Additional search-params for the new location
$router
β Store with shared routeInfo details, similar tolet:router
yrv
gracefully degrades tolocation.hash
on environments wherehistory
is not suitable, also it can be forced throughRouter.hashchange = true
.
Support for IE11 is granted if you include, at least, the following polyfills before your application:
<script>if (!!window.MSInputMethodContext && !!document.documentMode)
document.write('<script src="https://polyfill.io/v3/polyfill.min.js?features=default,Promise,Object.getOwnPropertyDescriptors"><\/script>');</script>
<script src="your-app.js"></script>
document.write()
is used because conditional comments were dropped in IE10, so this way you can conditionally load polyfills anyway.
Also, you MUST to enable either buble
or babel
within your build pipeline to transpile down to ES5.