2016-06-28 10 views
13

非ビューモデルクラス内の現在のルートを取得するには、ルータを挿入してthis.router.history.fragmentを使用することをお勧めしますか?それとも、これはノーですか?Aureliaで現在のルートを取得する

答えて

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

(も[コンポーネントを参照してくださいあなたが結合のためにそれを使用したいので、もし、あなたが 'created'方法でそれを得ることができ、' currentInstruction'はまだコンストラクタで 'null'なのであることに注意してくださいライフサイクル](http://aurelia.io/hub.html#/doc/article/aurelia/framework/latest/creating-components/3) –

+0

私は以前のルートのデータを得ています。 – IngoB

+0

おそらくビューがまだ変更されていない可能性があるので、あなたは 'canDeactivate'関数を持っているか確認してください。 –

関連する問題