如何从aurelia的网址获取参数

Pat*_*ick 3 aurelia

例如:http:// localhost:3000 /#/ report/123456

如何从Aurelia的网址中获取"123456"部分?

希望你能帮助我,在文档中找不到任何有用的东西.

小智 11

你可以在路由器的activate方法中获取提交的参数(在你的viewmodel中)

activate(params) {
   return this.http.fetch('contacts/' + params.id)
      .then(response => response.json())
      .then(contact => this.contact = contact);
}
Run Code Online (Sandbox Code Playgroud)

在一个很好的博客中找到:http://www.elanderson.net/2015/10/aurelia-routing-with-a-parameter/

  • 我想你可能会错过为这个`{route:['report /:id'],moduleId:'./ report',title:'Report',name:'report'}添加路由. (2认同)

Mik*_*ike 5

您必须为其定义一个路由:

{ 
    route: ['report/:id'], 
    moduleId: './report', 
    title: 'Report', 
    name: 'report' 
}
Run Code Online (Sandbox Code Playgroud)

然后在您的视图模型中,您可以idparams对象获取:

activate(params) {  
    console.log(params.id);
}
Run Code Online (Sandbox Code Playgroud)


小智 -8

var str = "http://localhost:3000/#/report/123456";
var res = str.split("/");
document.getElementById("demo").innerHTML = res[5];
Run Code Online (Sandbox Code Playgroud)