Aurelia: настройте и используйте fetch в main.js

Ранее я уже писал вопрос о том, как настроить клиент извлечения Aurelia и использовать настроенный клиент во всех моих viewModels ( Aurelia: настроить клиент извлечения). Я настраиваю клиента извлечения в моем файле main.js. Теперь я хочу использовать этот же настроенный клиент извлечения в 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');
    }
   });
 });
}

0 ответов

Другие вопросы по тегам