首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Aurelia加载路由动态/从获取

问Aurelia加载路由动态/从获取
EN

Stack Overflow用户
提问于 2016-06-06 00:02:08
回答 4查看 2.7K关注 0票数 1

我想动态加载菜单选项。所以我想知道最好的方法

我能够使用下面的代码在页面加载后添加路由。这适用于正常导航,但在刷新时不起作用。

配置路由器能否返回承诺/如何将菜单项加载到路由中?

代码语言:javascript
复制
 @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;
}
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-06-06 20:01:11

这不能回答你的问题,但我认为这可能对你和其他有类似问题的人有帮助。

动态路径反模式

您的应用程序有许多不同的路由,所有这些路由都根据应用程序的状态而有所不同。因此,您必须首先获取数据,然后构建路由,然后将它们注册到路由器。

这是反模式的原因是,当Aurelia本身使用静态方式描述动态内容时,您将需要根据应用程序的状态不断更新路由器。

动态路由同构数据

假设您正在构建Google,并且在用户添加和删除这些文件时,可能会更改许多不同的文件。对于这种情况,您有两类路由:文件夹和文档。因此,你为每条路都做了一条路。

代码语言:javascript
复制
configureRouter(config) {
    config.map([
        { route: 'folder/:id', moduleId: 'folder' }
        { route: 'document/:id', moduleId: 'document' }
    }
}

class FolderViewModel {
    activate({ id }) {

        // get your specific folder data and load it into your folder view model
        this.fetch('getDocuments?folderId=${id}')
    }   
}

class DocumentViewModel {
    activate({ id }) {

        // get your specific document and load it into your document view model
        this.fetch('getDocuments?documentId=${id}')
    }
}

动态路由异构数据

比方说,您想要构建YouTube。当用户mjd10d登录时,欢迎他用心观看视频内容,但他并不是一个优秀的内容创建者,也无法访问网站的内容创建部分。处理此问题的最佳方法是将所有可能的路由保留在应用程序中,并根据AuthorizeStep中的用户凭据对它们进行筛选。

代码语言:javascript
复制
configureRouter(config, router) {
  config.addPipelineStep('authorize', AuthorizeStep);
}

@inject(UserSession)
class AuthorizeStep {

  constructor(UserSession) {
    this.user = UserSession;
  }

  run(navigationInstruction, next) {
    var instructions = navigationInstruction.getAllInstructions()
    if (!this.authorized(instructions.config)) {
      return Redirect('404');
    }
    return next();
  }

  authorized(routeConfig) {

    // something smart that returns false if unauthorized
    return this.user.permissionLevel > routeConfig.requiredPermission;
  }
}

虽然并非所有情况都与授权相关,但您始终可以使用addPipelineStep API注册您自己的管道步骤。

票数 10
EN

Stack Overflow用户

发布于 2016-11-16 16:02:13

您可以通过在"configureRouter“方法(在app.ts中)中有一个固定(静态)路由动态添加路由(在启动时或在任何时间),然后在获取完成时动态添加所有其他路由,如下所示:

代码语言:javascript
复制
configureRouter(config, router) {
    config.title = 'SM';

    //configure one static route:
    config.map([
        { route: ['', 'welcome'], name: 'welcome', moduleId: 'welcome/welcome', title: 'Welcome' } 
    ]);

    routeMaps(this.navRepo) //your repo/service doing the async HTTP fetch, returning a Promise<Array<any>> (i.e., the routes)
        .then(r => {
            r.forEach(route => this.router.addRoute(route));
            //once all dynamic routes are added, refresh navigation:
            this.router.refreshNavigation();
        });

    this.router = router;
}

"routeMaps“函数只是回购调用的包装器,以及将结果映射到路由项数组的映射。

票数 1
EN

Stack Overflow用户

发布于 2017-01-24 09:10:36

你可以在激活中回复承诺。如果activate()返回允诺,configureRouter()将不会触发,直到activate()中返回的承诺被解析为止。

最后,我准备了激活路径,如下所示:

代码语言:javascript
复制
activate(){
    return this.http.fetch('url')
       .then(response => response.json())
       .then(docs => {    
           this.routerMapped = docs;
       });       
}

configureRouter(config, router) {
    //build the routes from this.routermapped if necessary
    config.map( this.routerMapped );
    this.router = router;  
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37647971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档