[英]Aurelia: During a Router's Pipeline Step, how do I bind a variable to that router?
I'd like to pass the user, found during the AuthorizeStep
to either the App class
and then to the home module
. 我想将在
AuthorizeStep
期间找到的用户传递给App class
,然后传递给home module
。
Here's what I have: 这就是我所拥有的:
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 my app I created a class called AuthContext with currentUser property. 在我的应用程序中,我创建了一个名为AuthContext的类,其中包含currentUser属性。 You can inject it in the constructor for the AuthorizeStep and then inject it in any other models that need it.
您可以将其注入AuthorizeStep的构造函数中,然后将其注入需要它的任何其他模型中。 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();
}
}
I have been doing something similar, but I found that I can't rely on the authcontext
being populated in other viewmodels by the time the viewmodel is being attached. 我一直在做类似的事情,但我发现在连接viewmodel时我不能依赖于在其他视图
authcontext
中填充的authcontext
。 Returning the promise returned by the get
and then returning next()
within the resolution of the get
seems to solve that, the idea being to not proceed to the next pipeline step until this one has resolved. 返回由返回的承诺
get
,然后返回next()
的分辨率内get
似乎解决了,这个想法是不进入下一个流水线步骤,直到这一个已经解决了。 Applying that to the answer from @JamesCarters, I'd get the following (untested) code: 将其应用于@JamesCarters的答案 ,我会得到以下(未经测试的)代码:
class AuthorizeStep {
static inject() { return [AuthContext];}
constructor(authcontext){
this.authContext = authcontext;
}
run(routingContext, next) {
if (routingContext.nextInstructions.some(i => i.config.auth)) {
return this.client.get('auth/login')
.then(response => {
this.authcontext.user = response.content;
return next();
});
}
else {
return next();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.