[英]Aurelia: During a Router's Pipeline Step, how do I bind a variable to that router?
我想將在AuthorizeStep
期間找到的用戶傳遞給App class
,然后傳遞給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();
}
}
在我的應用程序中,我創建了一個名為AuthContext的類,其中包含currentUser屬性。 您可以將其注入AuthorizeStep的構造函數中,然后將其注入需要它的任何其他模型中。 就像是...
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();
}
}
我一直在做類似的事情,但我發現在連接viewmodel時我不能依賴於在其他視圖authcontext
中填充的authcontext
。 返回由返回的承諾get
,然后返回next()
的分辨率內get
似乎解決了,這個想法是不進入下一個流水線步驟,直到這一個已經解決了。 將其應用於@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.