如何从aurelia的网址获取参数

时间:2016-04-22 07:46:33

标签: aurelia

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

如何从Aurelia的网址获取“123456”部分?

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

5 个答案:

答案 0 :(得分:11)

您可以在路由器的激活方法中获取提交的参数(在您的视图模型中)

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

在一篇不错的博文中找到:http://www.elanderson.net/2015/10/aurelia-routing-with-a-parameter/

答案 1 :(得分:5)

您必须为其定义路线:

{ 
    route: ['report/:id'], 
    moduleId: './report', 
    title: 'Report', 
    name: 'report' 
}

然后在您的视图模型中,您可以从id对象获取params

activate(params) {  
    console.log(params.id);
}

答案 2 :(得分:2)

import { Router } from 'aurelia-router'

通过依赖注入获取构造函数中的路由器,您可以使用它的值:

this.router.currentInstruction.params.myParam

答案 3 :(得分:2)

添加到Mike的答案中(不幸的是,我的积分还不够多):如果您希望参数是可选的,则可以在参数后面添加问号:

{ 
    route: ['report/:id?'], 
    moduleId: './report', 
    title: 'Report', 
    name: 'report' 
}

答案 4 :(得分:-7)

var str = "http://localhost:3000/#/report/123456";
var res = str.split("/");
document.getElementById("demo").innerHTML = res[5];
相关问题