Aurelia:在路由器的管道步骤中,如何将变量绑定到该路由器?

编程入门 行业动态 更新时间:2024-10-25 06:20:14
本文介绍了Aurelia:在路由器的管道步骤中,如何将变量绑定到该路由器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将在 AuthorizeStep 中找到的用户传递给 App类,然后转到主页模块。

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

推荐答案

In我的应用程序我创建了一个名为AuthContext的类,其中包含currentUser属性。您可以将其注入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:在路由器的管道步骤中,如何将变量绑定到该路由器?

本文发布于:2023-11-25 22:33:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由器   绑定   变量   如何将   到该

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!