Warning: Do not refresh this page as some parts of demo might not work!
Warning: Do not refresh this page as some parts of demo might not work!
npm i silkrouter rxjs
Initialize
const router = new Router();
Subscribe
router.subscribe((e) => {
console.log(e.route); // --> '/' (Current active route)
});
Trigger
router.set('/');
const router = new Router({
init: false, // Initialize on page load. Default: true
hashRouting: true, // Switch to hash routing. Default: false
preservePath: false, // If 'true' it will preserve current URL path. Works only for hash routing. Default: false
});
Click on one of the tabs below and observe how application reacts to route changes.
Click on the radio button below to enable hash routing.
Implementation
Hash routing implementation is straight forward.
// Create a new instance with hashRouting enabled
const hashRouter = new Router({
hashRouting: true
});
Route listener can accepts data via
This information is persisted in router event object. To demonstrate how this works, click on the button below:
This will activate a route "/tab3/:firstname/:lastname". Your data will appear below:
Implementation
To activate a route that accepts route parameters, silkrouter ships a
special operator called route
.
...
import { resolveParams } from 'silkrouter';
...
router.subscribe(e => {
const params = resolveParams('/tab3/:firstname/:lastname', e.route);
console.log(params);
});
Query strings on the other hand are passed to all listeners by default
(if they exist). You can access them via event.search
and
event.hashSearch
properties. You can also pass query
strings as data, and router will take care of the rest.
router.set('/tab3/john/doe', {
queryString: 'q=helloworld'
});
Similarly you can also pass data explicitly which is persisted in
history.state
object.
router.set('/tab3/john/doe', {
queryString: 'q=helloworld',
data: 'Hello World'
});
You can access this data using event.data
property.