Angularjs ';{';授权';:字符串;}';不可分配给类型';IHttpRequestConfigHeaders';

Angularjs ';{';授权';:字符串;}';不可分配给类型';IHttpRequestConfigHeaders';,angularjs,Angularjs,我正在尝试运行gulp命令,它给我以下错误: 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; }'. { TypeScr

我正在尝试运行gulp命令,它给我以下错误:

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);
        }
    }
}
//
/// 
模块核心服务{
导出类网络服务{
静态$inject:Array=['$http','$log','$q','appConstant','storageService'];
构造函数(public$http:ng.IHttpService、public$log:ng.ILogService、public$q:ng.IQService、,
public-appConstant:Shared.AppConstants,public-storageService:Services.storageService){}
private onError(错误:任意):无效{
//对所有错误(包括授权不动产)的通用处理
这是.$log.error(error);
}
私有getConfig(url:string,config?:ng.IRequestShortcutConfig):ng.IRequestConfig{
var httpConfig={};
如果(配置!=null){
extend(httpConfig,config);
}
var token=this.storageService.getItem(this.appConstant.keys.token,false);
if(令牌!=null){
var令牌头={
“授权”:“持票人”+代币
};
var currentHeaders=httpConfig.headers;
如果(当前标题){
httpConfig.headers=angular.extend(currentHeaders,tokenHeader);
}否则{
httpConfig.headers=tokenHeader;
}
}
httpConfig.url=url;
返回httpConfig;
}
私有getOrDelete(url:string,methodType:string,config?:ng.IRequestShortcutConfig):ng.IPromise{
var httpConfig=this.getConfig(url,config);
httpConfig.method=methodType;
返回此.getResponse(httpConfig);
}
私有getResponse(config:ng.IRequestConfig):ng.IPromise{
var deferred=这个$q.deferred();
config.headers
这是.http(config).success(
(结果:任何)=>{
延迟。解决(结果);
}).错误((错误,错误代码)=>{
这是一个错误;
deferred.reject(新的Core.Models.HttpError(error,errorCode));
});
回报。承诺;
}
获取(url:string,data?:any,config?:ng.IRequestShortcutConfig):ng.IPromise{
var httpConfig=this.getConfig(url,config);
httpConfig.method=“GET”;
如果(数据){
httpConfig.params=数据;
}
返回此.getResponse(httpConfig);
}
删除(url:string,data?:any,config?:ng.IRequestShortcutConfig):ng.IPromise{
var httpConfig=this.getConfig(url,config);
httpConfig.method=“删除”;
如果(数据){
httpConfig.params=数据;
}
返回此.getResponse(httpConfig);
}
post(url:string,data:any,config?:ng.IRequestShortcutConfig):ng.IPromise{
var httpConfig=this.getConfig(url,config);
httpConfig.method=“POST”;
httpConfig.data=jQuery.param(数据);
httpConfig.headers={
“内容类型”:“应用程序/x-www-form-urlencoded”
};
返回此.getResponse(httpConfig);
}
put(url:string,data:any,config?:ng.IRequestShortcutConfig):ng.IPromise{
var httpConfig=this.getConfig(url,config);
httpConfig.method=“PUT”;
httpConfig.data=数据;
返回此.getResponse(httpConfig);
}
修补程序(url:string,data:any,config?:ng.IRequestShortcutConfig):ng.IPromise{
var httpConfig=this.getConfig(url,config);
httpConfig.method=“PATCH”;
httpConfig.data=数据;
返回此.getResponse(httpConfig);
}
}
}

我已经安装了每个依赖项,但是如何才能删除此错误,是否应该安装更多的库?或者我应该删除任何东西使其工作吗?

从最后一条评论中,它说您需要首先将其转换为正确的字符串,因此您需要做的只是:

从'@angular/http'导入{Headers}

你需要做什么

let tokenHeader =  new Headers();
然后接着说:

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

这将清除错误消息。

您的
tokenHeader
包含
对象
您需要先将其转换为
字符串