Asked By: Anonymous
When using the aurelia.io framework router, what is the preferred method for reading and setting a query string?
For example, in the url: http://www.myapp.com/#/myroute1/?s=mystate
How do I read and set the ?s=mystate
part of the url and have the aurelia router navigate correctly and remember that state, such that whenever I arrive in my route1
viewmodel I can read that state variable and do something with it?
Solution
Answered By: Anonymous
On viewmodel you can implement method activate(params, routeConfig) and object params should contain your query variables
activate(params, routeConfig){
console.log(params.s); //should print mystate
}
To navigate to some route, you have to specify the name of this route in app.js (name: ‘redirect’)
config.map([
{ route: ['','welcome'], moduleId: './welcome', nav: true, title:'Welcome' },
{ route: 'flickr', moduleId: './flickr', nav: true, title:'Flickr' },
{ route: 'redirect', moduleId: './redirect', name: 'redirect'},
{ route: 'child-router', moduleId: './child-router', nav: true, title:'Child Router' }
]);
and then use method NavigateToRoute with parameters.
router.navigateToRoute('redirect', { s:'mystate'}, {replace: true});