2016-04-22 83 views

回答

-5
var str = "http://localhost:3000/#/report/123456"; 
var res = str.split("/"); 
document.getElementById("demo").innerHTML = res[5]; 
+0

這不是我的意思, 「123456」是一個變量,並且總是不同的。我需要知道如何將它放入變量/控制檯日誌中。 – Patrick

10

你可以在路由器的激活方法提交PARAMS(在視圖模型)

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/

+2

我想你可能會錯過爲此路由添加'{route:['report /:id'],moduleId:'./report',title:'Report',name:'report'},' –

+0

Here是討論params的相關文檔鏈接:http://aurelia.io/docs.html#/aurelia/framework/1.0.0-beta.1.2.2/doc/article/cheat-sheet/7 –

+1

@JohnManko:該鏈接不再有效,現在是http://aurelia.io/hub.html#/doc/article/aurelia/framework/latest/cheat-sheet/7 – eMBee

1
import { Router } from 'aurelia-router' 

獲取路由器在通過依賴注入的構造函數中,您可以使用它的值:

this.router.currentInstruction.params.myParam 
2

你已經爲它定義一個路線:

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

然後在您的視圖模型,你可以從params對象得到id

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