简体   繁体   English

Aurelia - 等待承诺在路由器预呈现步骤中返回

[英]Aurelia - waiting for a promise to return in the router prerender step

TL;DR - How can I delay the ProcessResult() function in aurelia-router.js from executing because I'm still waiting on the result of a promise in my code? TL; DR - 如何延迟aurelia-router.jsProcessResult()函数执行,因为我还在等待代码中的promise的结果? It results in the right module rendering, with the wrong address/href. 它导致正确的模块渲染,错误的地址/ href。

Example: if you're on base-module, then click on admin, the admin module will load, but the href remains www.mycompany.com/#/base-module instead of www.mycompany.com/#/admin , and this error can be seen in the console: 示例:如果您在基本模块上,然后单击管理员,管理模块将加载,但href仍然是www.mycompany.com/#/base-module而不是www.mycompany.com/#/admin ,并且在控制台中可以看到此错误:

ERROR [app-router] Error: Expected router pipeline to return a navigation result, but got [{}] instead 错误[app-router]错误:预期路由器管道返回导航结果,但得到[{}]

Longer version: 更长的版本:

I have a prerender step in my router that checks whether the user is enabled for a particular module before rendering the view. 我在路由器中有一个预呈现步骤,用于检查在呈现视图之前是否为特定模块启用了用户。

In my PreRenderStep class I have a run function, which calls a mediator to get the permissions for a user, then checks whether the module the user clicked on is in their enabled modules list. 在我的PreRenderStep类中,我有一个运行函数,它调用一个中介来获取用户的权限,然后检查用户点击的模块是否在其启用的模块列表中。 The call to the mediator involves a promise. 对调解员的呼吁涉及承诺。

The problem is that the run promise in the prerender step resolves before a promise within the run method finishes. 问题是prerender步骤中的run promise在run方法中的promise完成之前解析。 Therefore, the view is eventually rendered (because the user is enabled) but the previous href remains in the address bar. 因此,最终会呈现视图(因为用户已启用),但之前的href仍保留在地址栏中。

Router: 路由器:

  configureRouter(config, router) { config.title = "Tramonex"; config.addPipelineStep('authorize', AuthorizeStep); config.addPreRenderStep(PreRenderStep); config.map([ { route: ['', 'log-in-out'], name: 'home', moduleId: 'modules/authentication/log-in-out' }, { route: 'passwordReset', moduleId: 'modules/authentication/password-reset', }, {route: 'app', moduleId: 'app', auth: true}, { route: 'base-module', name: 'base-module', moduleId: 'modules/base-module', href: 'base-module', nav: true, auth: true }, { route: 'test1', name: 'test1', moduleId: 'modules/test1/test1', href: 'test1', nav: true, auth: true, settings: {moduleAuthRequired: true} }, { route: 'test2', name: 'test2', moduleId: 'modules/test2/test2', href: 'test2', nav: true, auth: true, settings: {moduleAuthRequired: true} }, { route: 'admin', name: 'admin', moduleId: 'modules/admin/admin', href: 'admin', nav: true, auth: true, settings: {moduleAuthRequired: true} }, ]); this.router = router; } } 

PreRenderStep: PreRenderStep:

 @inject(Mediator, AuthenticationService) class PreRenderStep { constructor(mediator, authenticationService) { this.mediator = mediator; this.authenticationService = authenticationService; } run(navigationInstruction, next) { if (navigationInstruction.getAllInstructions().some(i => i.config.settings.moduleAuthRequired)) { this.redirect = false; this.mediator.getPermissionsForUser() .then(user => { userPerms = user.modules; var isEnabled = userPerms.includes(navigationInstruction.config.name); if (!isEnabled) { this.redirect = true; } }) .then(() => { return this.redirect next.cancel(navigationInstruction.router.navigateToRoute('base-module')) : next(); }); } else { return next(); } } } 

When the user clicks on a module that requires an auth check, the code is hit and whilst we're waiting on the promise from the mediator.getPermissionsForUser() to return, this code in aurelia-router.js is hit (the lines with stars): 当用户点击需要验证的模块时,代码被命中,当我们等待来自mediator.getPermissionsForUser()的promise返回时,aurelia-router.js中的代码被命中(带有星):

 function processResult(instruction, result, instructionCount, router) { if (!(result && 'completed' in result && 'output' in result)) { result = result || {}; **result.output = new Error('Expected router pipeline to return a navigation result, but got [' + JSON.stringify(result) + '] instead.');** } var finalResult = null; if (isNavigationCommand(result.output)) { result.output.navigate(router); } else { finalResult = result; if (!result.completed) { if (result.output instanceof Error) { logger.error(result.output); } **restorePreviousLocation(router);** } } 

您需要返回在run函数中创建的promise。

return this.mediator.getPermissionsForUser()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM