I want update the url-params in the address bar without routing. But i'm not sure how to do this with Aurelia-router from a view-model.
In my case I send IDs in the url which gets picked up by the view-model's activate-method.
The route looks like this: http://localhost:3000/#/test/products?0=2599037842&1=2599080552
Then I want to be able to remove IDs from the url without reactivating the view-model, url result exemple: http://localhost:3000/#/test/products?0=2599037842
Hopefully there is support for this in Aurelia-router
Thanks! /Mike
To use Aurelia's router, your component view must have a <router-view></router-view> element. In order to configure the router, the component's view-model requires a configureRouter () function. config.map () adds route (s) to the router.
In app.js a new route was added for the detail view. Make special note that the new route expects an id parameter. In list.html the contacts need to be rendered with an anchor that will lead to the proper detail view. To accomplish this Aurelia has a route-href custom attribute that can be used with an anchor.
A RouteConfig object. A Promise that resolves to any of the above. Aurelia allows redirecting of routes to URL fragments by specifying redirect with a string consisting of a URL fragment. The redirect is particularly useful when you have an "empty" route pattern (such as the first route above) that maps to a component with a child router.
Aurelia allows you to map any unknown routes. Parameters passed to mapUnknownRoutes () can be: A string to a moduleId. This module will be navigated to any time a route is not found. A routeConfig object. This configuration object will be used any time a route is not found.
Yes, you can do that with router.navigateToRoute()
method. navigateToRoute
has additional parameters. Use options
(third) parameter to modify how the navigation is done.
Example:
import {inject} from 'aurelia-framework';
import {Router} from 'aurelia-router';
@inject(Router)
export class Products {
constructor(router) {
this.router = router;
}
activate(params) {
// TODO: Check your params here and do navigate according to values
this.router.navigateToRoute(
this.router.currentInstruction.config.name, // current route name
{ '0': params['0'] }, // route parameters object
{ trigger: false, replace: true } // options
);
}
}
From documentation hub:
navigateToRoute(route: string, params?: any, options?: any): boolean
Navigates to a new location corresponding to the route and params specified.
Params
route: string
- The name of the route to use when generating the navigation location.params?: any
- The route parameters to be used when populating the route pattern.options?: any
- The navigation options.
With options
you control how the history is updated.
trigger: false
- prevents the router navigation pipeline to be triggeredreplace: true
- replaces the current URL in history with provided route (rewriting history), so it won't be triggered with browser back functionalityIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With