Router package for Frint
With npm:
$ npm install --save frint-router
Via unpkg CDN:
<script src="https://unpkg.com/frint-router@latest/dist/frint-router.min.js"></script>
<script>
// available as `window.FrintRouter`
</script>
The classes exported by this package are all independent and can be used as is.
import HashRouterService from 'frint-router/HashRouterService';
// import BrowserRouterService from 'frint-router/BrowserRouterService';
// import MemoryRouterService from 'frint-router/MemoryRouterService';
const router = new HashRouterService();
const subscription = router.getHistory$().subscribe(function (history) {
console.log(history);
// {
// length: 1,
// location: { ... }, // Object liked `window.location`
// action: 'PUSH',
// }
});
The subscription will keep emitting new values every time there is a change in history.
To connect it well with other packages, we need to follow a convention of using the provider name router:
import { createApp } from 'frint';
import HashRouterService from 'frint-router/HashRouterService';
const RootApp = createApp({
name: 'MyRootApp',
providers: [
{
name: 'router',
useFactory: function () {
return new HashRouterService();
},
cascade: true,
}
],
});
It is advised to import the appropriate router service class directly from the package, to make sure you are only bundling the services you explicitly need.
// will bundle only the individual HashRouterService
import HashRouterService from 'frint-router/HashRouterService';
// will bring ALL the service classes
import { HashRouterService } from 'frint-router';
The package exports three classes:
BrowserRouterService: uses HTML5 History APIHashRouterService: For legacy browsersMemoryRouterService: useful for testsAll of them implement the same set of methods.
options (Object)options.enableCache (Boolean): Enables caching, set to true by default.options.cacheLimit (Number): Maximum limit of entries to cache, set to 10000 by default.getHistory$()
Streams history as it changes over time.
Observable: Streams history Object.
Structure of history object:
{
length: 1, // number of entries
location: { ... }, // like `window.location`
action: 'PUSH' // either PUSH, REPLACE, or POP
}
getMatch$(pattern, options)
Keeps matching pattern against history as it keeps changing over time.
pattern (String): Pattern to match againstoptions (Object)options.exact (Boolean): Matches pattern exactly, defaults to false.Observable: Streams null if nothing matched, otherwise a matched object.
A matched object follows this structure:
{
url: '/',
isExact: true,
params: {
key: 'value',
}
}
getMatch(pattern, history, options)
Synchronous way of matching pattern against provided history.
pattern (String): Pattern to match againsthistory (Object): History objectoptions (Object)options.exact (Boolean)Returns null when nothing matched, or a matched object.
go(n)
n (Number)void
push(path, state)
path (String)state (Object)void
replace(path, state)
path (String)state (Object)void
goBack()
Equivalent to go(-1)
void
goForward()
void
Same as above.
Same as above.