How do I set/read a query string when using the router in aurelia?

前端 未结 3 1710
时光说笑
时光说笑 2021-02-05 16:35

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/#/my

3条回答
  •  庸人自扰
    2021-02-05 17:17

    If you want to change queryParams and reload page with it (e.g. change page number on pagination link click) -- you must use determineActivationStrategy with replace.

    Create view model and add determineActivationStrategy function:

    import {activationStrategy} from 'aurelia-router';
    export class ModelView{
    
      determineActivationStrategy() {
        return activationStrategy.replace;
      }
    
    }
    

    Add activate event to your model for saving params:

    activate(params, routeConfig, navigationInstruction){
        this.queryParams = params ? params : {};
    }
    

    Add code for reload or navigate with same or new parameters:

    moveToNewPage(newPageNumber){
      this.queryParams.page = newPageNumber; // change page param to new value
      this.router.navigateToRoute(this.router.currentInstruction.config.name, this.queryParams);
    }
    

    P.S. for this.router access use inject and don't forget to set name of router in app configuration:

    import {Router} from 'aurelia-router';
    export class ModelView{
      static inject() { return [Router] };
    
      constructor(router){
        this.router = router;
      }
    }
    

提交回复
热议问题