【发布时间】:2017-02-17 21:51:24
【问题描述】:
我想动态加载菜单选项。所以我想知道最好的方法
我可以在页面加载后使用下面的代码添加路由。这适用于正常导航,但在刷新期间不起作用。
可以配置路由器返回一个承诺/我如何将菜单项加载到路由中?
@inject(HttpClient)
export class DocumentMenu {
router: Router;
documents : IDocument[];
heading = 'Document Router';
constructor(public http: HttpClient) {}
activate(): void {
this.http.fetch('http://localhost:17853/Document/GetDocuments?folderID=13244')
.then<IDocument[]>(response => response.json())
.then<IDocument[]>(docs => {
if ( docs ){
for( var doc of docs){
this.router.addRoute( { route : doc.DocumentID.toString(), name : doc.Name, moduleId: './documents/document', nav:true, title: doc.Name });
}
this.router.refreshNavigation();
}
return docs;
});
}
configureRouter(config: RouterConfiguration, router: Router) {
var routes = new Array();
routes.push(
{ route: 'index', name: 'index-name', moduleId: './documents/index', nav: false, title: 'Documents' } );
routes.push( { route: '', redirect: 'index' } );
config.map( routes );
this.router = router;
}
}
【问题讨论】:
-
可以静态添加所有路由,而不是动态添加路由,然后使用授权管道检查用户是否可以访问它
-
@FabioLuz 可以工作,除非我不知道路线是什么。它有点像一个浅树结构,用户可以在其中选择一个包含 0 到 15 个文档的文件夹。我希望文档成为导航选项
-
有没有试过在构造函数中做?
-
@Matt-McCabe 刚刚做了,效果很好,将发布答案
标签: aurelia