在 aurelia 中使用路由器时如何设置/读取查询字符串? [英] How do I set/read a query string when using the router in aurelia?

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

问题描述

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

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

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

我如何读取和设置 url 的 ?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?

推荐答案

在视图模型上你可以实现方法 activate(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 中使用路由器时如何设置/读取查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆