Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aurelia-Router: Modify route params and address bar from VM with Router

Tags:

aurelia

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

like image 633
Mike Avatar asked Aug 31 '16 08:08

Mike


People also ask

How do I configure the router in Aurelia?

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.

How to create a detail view in Aurelia using a route?

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.

How to redirect a route to a URL fragment in Aurelia?

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.

How do I map an unknown route in Aurelia?

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.


1 Answers

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 triggered
  • replace: true - replaces the current URL in history with provided route (rewriting history), so it won't be triggered with browser back functionality
like image 192
Miroslav Popovic Avatar answered Nov 02 '22 06:11

Miroslav Popovic