2017-05-18 2 views
0

Ich versuche, schluck Befehl auszuführen und es gibt mir die folgende Fehlermeldung:'{' Autorisierung ': Zeichenfolge; }‘Ist nicht übertragbar auf den Typ‚IHttpRequestConfigHeaders‘

src\core\services\NetworkService.ts(36,21): error TS2322: Type '{ 'Authorization': string; }' is not assignable to type 'IHttpRequestConfigHeaders'. 
    Index signature is missing in type '{ 'Authorization': string; }'. 
{ TypeScript error: src\core\services\NetworkService.ts(36,21): error TS2322: Type '{ 'Authorization': string; }' is not assignable to type 'IHttpRequestConfigHeaders'. 

NetwrokService.ts

/// <reference path="../../../typings/app.d.ts" /> 
/// <reference path="../../../typings/tsd.d.ts" /> 


module Core.Services { 

    export class NetworkService { 
     static $inject: Array<string> = ['$http', '$log', '$q', 'appConstant', 'storageService']; 

     constructor(public $http: ng.IHttpService, public $log: ng.ILogService, public $q: ng.IQService, 
      public appConstant: Shared.AppConstants, public storageService: Services.StorageService) { } 

     private onError(error: any): void { 
      // generic handling for all error, including authorization realted stuff 
      this.$log.error(error); 
     } 

     private getConfig(url: string, config?: ng.IRequestShortcutConfig): ng.IRequestConfig { 
      var httpConfig = <ng.IRequestConfig>{}; 
      if (config != null) { 
       angular.extend(httpConfig, config); 
      } 

      var token = this.storageService.getItem(this.appConstant.keys.token, false); 
      if (token != null) { 

       var tokenHeader = { 
        'Authorization': "Bearer " + token 
       }; 

       var currentHeaders = httpConfig.headers; 
       if (currentHeaders) { 
        httpConfig.headers = angular.extend(currentHeaders, tokenHeader); 

       } else { 
        httpConfig.headers = tokenHeader; 
       } 
      } 

      httpConfig.url = url; 
      return httpConfig; 
     } 

     private getOrDelete<T>(url: string, methodType: string, config?: ng.IRequestShortcutConfig): ng.IPromise<T> { 

      var httpConfig = this.getConfig(url, config); 
      httpConfig.method = methodType; 
      return this.getResponse<T>(httpConfig); 
     } 

     private getResponse<T>(config: ng.IRequestConfig): ng.IPromise<T> { 
      var deferred = this.$q.defer(); 

      config.headers 
      this.$http(config).success(
       (result: any) => { 
        deferred.resolve(result); 
       }).error((error, errorCode) => { 
        this.onError(error); 
        deferred.reject(new Core.Models.HttpError(error, errorCode)); 
       }); 


      return deferred.promise; 
     } 


     get<T>(url: string, data?: any, config?: ng.IRequestShortcutConfig): ng.IPromise<T> { 

      var httpConfig = this.getConfig(url, config); 
      httpConfig.method = "GET"; 
      if (data) { 
       httpConfig.params = data; 
      } 
      return this.getResponse(httpConfig); 
     } 


     delete<T>(url: string, data?: any, config?: ng.IRequestShortcutConfig): ng.IPromise<T> { 

      var httpConfig = this.getConfig(url, config); 
      httpConfig.method = "DELETE"; 
      if (data) { 
       httpConfig.params = data; 
      } 
      return this.getResponse(httpConfig); 
     } 


     post<T>(url: string, data: any, config?: ng.IRequestShortcutConfig): ng.IPromise<T> { 

      var httpConfig = this.getConfig(url, config); 
      httpConfig.method = "POST"; 
      httpConfig.data = jQuery.param(data); 
      httpConfig.headers = { 
       'Content-Type': 'application/x-www-form-urlencoded' 
      }; 
      return this.getResponse<T>(httpConfig); 
     } 

     put<T>(url: string, data: any, config?: ng.IRequestShortcutConfig): ng.IPromise<T> { 

      var httpConfig = this.getConfig(url, config); 
      httpConfig.method = "PUT"; 
      httpConfig.data = data; 
      return this.getResponse<T>(httpConfig); 
     } 

     patch<T>(url: string, data: any, config?: ng.IRequestShortcutConfig): ng.IPromise<T> { 

      var httpConfig = this.getConfig(url, config); 
      httpConfig.method = "PATCH"; 
      httpConfig.data = data; 
      return this.getResponse<T>(httpConfig); 
     } 
    } 
} 

ich jede Abhängigkeit installiert haben, aber wie kann ich diesen Fehler entfernen, sollte ich weitere Bibliotheken installieren? oder sollte ich etwas entfernen, damit es funktioniert?

+0

Ihr 'tokenHeader' enthält ein' Objekt', das Sie zuerst in 'string' konvertieren müssen. –

Antwort

0

Aus dem letzten Kommentar, es sagt, Sie müssen es zuerst auf die Zeichenfolge konvertieren, die alles, was Sie tun müssen, richtig so ist:

import { Headers } from '@angular/http';

und wo Sie die tokenHeader tun

brauchen
let tokenHeader = new Headers(); 

und folgen Sie anschließend mit:

tokenHeader.append('Authorization', 'Bearer'+ this.token); 

Dies sollte die Fehlermeldung löschen.

Verwandte Themen