13
要獲得非視圖模型類中的當前路由,最佳做法是注入路由器並使用this.router.history.fragment?或者這是否定的?獲取Aurelia中的當前路由
要獲得非視圖模型類中的當前路由,最佳做法是注入路由器並使用this.router.history.fragment?或者這是否定的?獲取Aurelia中的當前路由
您可以注入路由器並獲取當前指令。就像這樣:
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
}
}
請注意,'currentInstruction'仍然'在構造null',所以如果你想使用它的綁定,可以在'created'方法得到它(另請參見[成分生命週期](http://aurelia.io/hub.html#/doc/article/aurelia/framework/latest/creating-components/3) –
我在這裏得到以前的路線的數據,任何想法爲什麼?謝謝 – IngoB
可能是因爲視圖還沒有改變檢查你是否有'canDeactivate'函數 –