2016-06-28 106 views

回答

20

您可以注入路由器並獲取當前指令。就像這樣:

import { inject } from 'aurelia-dependency-injection'; //or framework 
import { Router } from 'aurelia-router'; 

@inject(Router) 
export class MyClass { 

    constructor(router) { 
     this.router = router; 
    } 

    getRoute() { 
    return this.router.currentInstruction.config.name; //name of the route 
    //return this.router.currentInstruction.config.moduleId; //moduleId of the route 
    } 
} 
+5

請注意,'currentInstruction'仍然'在構造null',所以如果你想使用它的綁定,可以在'created'方法得到它(另請參見[成分生命週期](http://aurelia.io/hub.html#/doc/article/aurelia/framework/latest/creating-components/3) –

+0

我在這裏得到以前的路線的數據,任何想法爲什麼?謝謝 – IngoB

+0

可能是因爲視圖還沒有改變檢查你是否有'canDeactivate'函數 –