本文介绍了在aurelia中使用路由器时如何设置/读取查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用aurelia.io框架路由器时,读取和设置查询字符串的首选方法是什么?

When using the aurelia.io framework router, what is the preferred method for reading and setting a query string?

例如,在url中: http://www.myapp.com/#/myroute1/?s=mystate

如何阅读和设置?s = mystate 部分网址并让aurelia路由器正确导航并记住该状态,这样每当我到达 route1 viewmodel我可以读取那个状态变量并用它做一些事情吗?

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?

推荐答案

在viewmodel上你可以实现方法激活( params,routeConfig)和对象参数应该包含你的查询变量

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
}

要导航到某个路线,你必须指定app.js中此路线的名称(名称:'redirect')

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

然后使用方法NavigateToRoute和参数。

and then use method NavigateToRoute with parameters.

router.navigateToRoute('redirect', { s:'mystate'}, {replace: true});

这篇关于在aurelia中使用路由器时如何设置/读取查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 09:22