2016-06-28 10 views
13

Geçerli rotayı görünüm dışı model sınıfında almak için, yönlendiriciyi enjekte etmek ve this.router.history.fragment'i kullanmak en iyi yöntem olabilir mi? Yoksa bu hayır değil mi?Geçerli rotayı Aurelia'da alın

cevap

20

Yönelticiyi enjekte edebilir ve mevcut talimatları alabilirsiniz. Şunun gibi:

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' 'hala kurucusundaki null' olduğunu' lütfen unutmayın, bu nedenle bağlayıcı için kullanmak istiyorsanız, [bileşen ayrıca bkz ('created' yönteminde alabilirsiniz yaşam döngüsü] (http://aurelia.io/hub.html#/doc/article/aurelia/framework/latest/creating-components/3) –

+0

Burada önceki yolun verilerini alıyorum, herhangi bir fikir neden ??? Teşekkürler – IngoB

+0

Muhtemelen görünüm henüz değişmediği için bir "canDeactivate" işlevi olup olmadığını kontrol edin –

İlgili konular