Aurelia:在路由器的流水线步骤中,如何将变量绑定到该路由器? [英] Aurelia: During a Router's Pipeline Step, how do I bind a variable to that router?
本文介绍了Aurelia:在路由器的流水线步骤中,如何将变量绑定到该路由器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将在 AuthorizeStep
中找到的用户传递给 App class
,然后传递给 home 模块
.
I'd like to pass the user, found during the AuthorizeStep
to either the App class
and then to the home module
.
这是我所拥有的:
export class App {
configureRouter(config, router) {
config.addPipelineStep('authorize', AuthorizeStep);
config.map([
{route: ['', ':filter'], name: "", moduleId: 'welcome'}
{route: 'home', name: "home", moduleId: 'home' auth:true}
]);
this.router = router;
}
}
class AuthorizeStep {
run(routingContext, next) {
if (routingContext.nextInstructions.some(i => i.config.auth)) {
this.client.get('auth/login')
.then(response => {
this.user = response.content;
});
}
return next();
}
}
推荐答案
在我的应用程序中,我使用 currentUser 属性创建了一个名为 AuthContext 的类.您可以将它注入到 AuthorizeStep 的构造函数中,然后将它注入到需要它的任何其他模型中.像……
In my app I created a class called AuthContext with currentUser property. You can inject it in the constructor for the AuthorizeStep and then inject it in any other models that need it. Something like...
import {AuthContext} from './auth-context';
export class App {
static inject() { return [AuthContext];}
constructor(authcontext){
this.authContext = authcontext;
}
configureRouter(config, router) {
config.addPipelineStep('authorize', AuthorizeStep);
config.map([
{route: ['', ':filter'], name: "", moduleId: 'welcome'}
{route: 'home', name: "home", moduleId: 'home' auth:true}
]);
this.router = router;
}
}
class AuthorizeStep {
static inject() { return [AuthContext];}
constructor(authcontext){
this.authContext = authcontext;
}
run(routingContext, next) {
if (routingContext.nextInstructions.some(i => i.config.auth)) {
this.client.get('auth/login')
.then(response => {
this.authcontext.user = response.content;
});
}
return next();
}
}
这篇关于Aurelia:在路由器的流水线步骤中,如何将变量绑定到该路由器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文