gpt4 book ai didi

javascript - Aurelia:在路由器的流水线步骤中,我如何将变量绑定(bind)到该路由器?

转载 作者:数据小太阳 更新时间:2023-10-29 05:05:35 26 4
gpt4 key购买 nike

我想将在 AuthorizeStep 期间找到的用户传递给 App 类,然后传递给 home 模块

这是我所拥有的:

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();
}
}

最佳答案

在我的应用程序中,我创建了一个名为 AuthContext 的类,它具有 currentUser 属性。您可以将它注入(inject)到 AuthorizeStep 的构造函数中,然后将它注入(inject)到任何其他需要它的模型中。像...

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();
}
}

关于javascript - Aurelia:在路由器的流水线步骤中,我如何将变量绑定(bind)到该路由器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31425138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com