2017-05-18 15 views
0

私は一口コマンドを実行しようとしていますし、それは私に次のエラー与える:'{'承認 ':文字列; }」型に代入できません 『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); 
     } 
    } 
} 

私はすべての依存関係をインストールしましたが、これ以上のライブラリをインストールする必要がありますが、どうすればこのエラーを取り除くことができますか?それを機能させるために何かを削除する必要がありますか?

+0

あなたの 'tokenHeader'は' string'に最初に変換する必要がある 'object'を含んでいます。 –

答えて

0

最後のコメントから、それはあなたがする必要があるすべては、あなたが最初に右のように文字列に変換する必要があると述べている:

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

、あなたはtokenHeaderが

を行う必要があります
let tokenHeader = new Headers(); 

し、次いでその後、次のとおりです。

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

これは、エラーメッセージをクリアするはずです。

関連する問題