我之前发过一个关于如何配置Aurelia的fetch客户端并在我的所有viewModel中使用配置的客户端的问题(Aurelia: configure fetch client) 我在main.js文件中配置了我的fetch客户端。现在我想在main.js本身中使用这个相同的配置提取客户端。我无法注入客户端的实例,因为它将在注入后定义。
import {HttpClient} from 'aurelia-fetch-client';
export function configure(aurelia) {
this.jsonobj = {
'operation':'verify'
};
aurelia.use
.standardConfiguration()
.developmentLogging();
let container = aurelia.container;
let http = new HttpClient();
http.configure(config => {
config
.useStandardConfiguration()
.withInterceptor({
request(request) {
request.headers.set('token', window.localStorage.getItem('token'));
return request;
}
});
});
container.registerInstance(HttpClient, http);
aurelia.start().then(
a => {
this.http.fetch('dist/components/api.php', {
credentials: 'include',
method: 'post',
body: JSON.stringify(this.jsonobj)
})
.then(response => response.json())
.then(data => {
if (data.error==='true') {
a.setRoot('login');
}else {
a.setRoot('app');
}
});
});
}