The app.router
is part of the boiler plater and immediately available. It allows to lazy-load ES6
modules.
Adds a route. You can add multiple at the same time if you pass an object to add
method, where key is the route and value is its callBack
import router from './router.js'
// single route
router.add("/home", (route, params) => {
console.log(route, params);
})
// many routes at once
router.add({
// exact route
"/home"(route, params) {
console.log(route, params);
},
// wild card route
"/home/*"(route, params) {
console.log(route, params);
},
// route with variables
"/home/:id/view/:class"(route, params) {
console.log(route, params);
}
})
Returns currnet route.
Navigate to the route and triggers change enent.
import router from './router.js'
...
router.go('/my/new/route')
Initializes router module and sets defaultRoute. Used once in app/start.js
Returns all registered routes.
Removes specified route.
Sets route silently without triggering change events.
Events are only available is you include w2ui as it takes them from w2utils.
Event that is triggered when a new route is added. Callback function will receive an event object with additional information.
import router from './router.js'
router.on('add', (event) => {
console.log('A new route is added', event)
})
Event that is triggered when a route is removed. Callback function will receive an event object with additional information.
Event that is triggered when a route is processed. Callback function will receive an event object with additional information
import router from './router.js'
router.on('route', (event) => {
console.log('A route just got triggered', event)
})