TL返回; DR - 如何從執行延緩aurelia-router.js
的ProcessResult()
功能,因爲我仍然在我的代碼承諾的結果,還等什麼?它會導致正確的模塊渲染,並帶有錯誤的地址/ href。奧裏利亞 - 等待承諾的路由器預渲染一步
例如:如果你的基礎模塊上,然後單擊管理員,管理員模塊將加載,但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
加長版:
我有我的路由器預渲染的步驟,檢查用戶是否被渲染視圖之前特定模塊啓用。
在我的PreRenderStep類中,我有一個運行函數,該函數調用中介以獲取用戶的權限,然後檢查用戶單擊的模塊是否在其啓用的模塊列表中。對調解人的呼籲涉及承諾。
問題在於預渲染步驟中的運行承諾在run方法內的承諾完成之前解決。因此,該視圖最終呈現(因爲用戶已啓用),但之前的href保留在地址欄中。
路由器:
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:
@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();
}
}
}
當用戶點擊需要身份驗證檢查的模塊時,代碼被打中,並且在我們等待來自mediator.getPermissionsForUser()的承諾返回時,此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);**
}
}
如果您從prerender步驟返回承諾,它是否正常工作:return this.mediator.getPermissionsForUser()。then ...? – mgiesa
也有一個授權的步驟,運行第一感覺就像做此項檢查 – mgiesa
[這個答案](http://stackoverflow.com/a/36339620/3478010)建議你在正確的地方都可能只是缺少一個'return' ,即'返回this.mediator.getPermissionsForUser()。然後,(...)' –