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

前端 未结 3 1860
时光说笑
时光说笑 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:12

    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});
    

提交回复
热议问题