Aurelia main.js中setRoot处的依赖项注入(用于HttpFetch)
我无法让依赖项注入为我的AuthorizerService工作。显然,DepInj直到Aurelia“启动”后才准备好,但我不确定如何访问它 main.js:Aurelia main.js中setRoot处的依赖项注入(用于HttpFetch),aurelia,Aurelia,我无法让依赖项注入为我的AuthorizerService工作。显然,DepInj直到Aurelia“启动”后才准备好,但我不确定如何访问它 main.js: aurelia.container.registerInstance(HttpClient, http.c()); // set your interceptors to take cookie data and put into header return aurelia.start().then(() => {
aurelia.container.registerInstance(HttpClient, http.c());
// set your interceptors to take cookie data and put into header
return aurelia.start().then(() => {
let Authorizer = new AuthorizerService();
aurelia.container.registerInstance(AuthorizerService, Authorization);
console.log('Current State: %o', Authorizer.auth);
Authorizer.checkCookieAndPingServer().then(() => { console.log('Current State: %o', Authorizer.auth); aurelia.setRoot(PLATFORM.moduleName('app')); }, () => { aurelia.setRoot(PLATFORM.moduleName('login-redirect')); });
});
return aurelia.start().then(() => {
let Authorizer = aurelia.container.get(AuthorizerService);
let root = Authorizer.isAuthenticated() ? PLATFORM.moduleName('app') : PLATFORM.moduleName('login');
console.log('Current State: %o', Authorizer.auth);
aurelia.setRoot(root);
});
现在的问题是,如果我执行“newauthorizerservice()”,那么“this.http.fetch()”在AuthorizerService.js中不可用
我是否打算将“http.c()”(它传递HttpClient实例)作为参数传递到:
checkCookieAndPingServer(http.c())
还是有别的办法
我是否可以删除“new AuthorizerService()”并执行以下操作(这是我编造的):
以某种方式强制它进行依赖项注入并检索“http.c()”的“已注册实例”
我不能只检查饼干。我必须ping服务器以确保安全,服务器将设置cookie
我认为这是各种各样的错误,因为我需要一个默认为false的全局参数,然后它对后端服务器进行查询并相应地设置根。可能只在“登录页面”中查询后端?好的,但我需要在登录模块中执行“setRoot(backtoApp);aurelia.AlsoSetLoggedIn(true);”。但是当我设置root(backtoApp)时,它又重新开始了
换句话说,当setRoot(登录)时;然后设置根(backToApp) 如果要获取实例的类纯粹基于服务类,并且不依赖于某些Aurelia插件,则不需要等到Aurelia开始安全地调用容器 例如: aurelia.container.getInstance(AuthorizerService); 可能是 aurelia.container.get(AuthorizerService)
正如您在问题中所注意到的那样,您不应该使用
新的AuthorizerService()
。是的,我上面写的“编辑:更好的解决方案”现在看起来一切都正常。这似乎是不破坏依赖注入的最好方法。
return aurelia.start().then(() => {
let Authorizer = aurelia.container.get(AuthorizerService);
let root = Authorizer.isAuthenticated() ? PLATFORM.moduleName('app') : PLATFORM.moduleName('login');
console.log('Current State: %o', Authorizer.auth);
aurelia.setRoot(root);
});
constructor(http) {
this.http = http;
this.auth = {
isAuthenticated: false,
user: {}
}
}