2016-09-30 53 views
0

我正在使用angularcript 1.5与typescript,并且我想创建一个拦截器来在自定义头中插入令牌,并且还从每个响应中更新它。在严格模式下Angular Typescript依赖注入显式注解错误

在运行时,我得到下面的异常:

Uncaught Error: [$injector:strictdi] function($q) is not using explicit annotation and cannot be invoked in strict mode

class AuthInterceptor implements ng.IHttpInterceptor { 

    $q:ng.IQService; 

    static $inject = ['$q']; 
    constructor($q:ng.IQService) { 
    this.$q = $q; 
    } 

    static factory($q:ng.IQService):AuthInterceptor { 
    return new AuthInterceptor($q); 
    } 


    request = (config:ng.IRequestConfig):ng.IRequestConfig => { 
    // config.headers = config.headers || {}; 
    console.info('Request config md', config); 
    // config.headers['token'] = 'test token'; 

    return config; 
}; 

    response = <T>(response: ng.IHttpPromiseCallbackArg<T>):ng.IPromise<T> => { 
    console.info('Response:', response); 

// modify response 

    return this.$q.when(response); 
}; 

}

let httpConfig = ($httpProvider:ng.IHttpProvider) => { 
    $httpProvider.interceptors.push(AuthInterceptor.factory); 
}; 

angular.module('app').config(httpConfig); 

你有什么想法,我怎么能解决这个问题?

在此先感谢。

回答

0

请试试这个。这是每个请求最简单的拦截器

app.factory('myInterceptor', function() { 
     var requestInterceptor = { 
      request: function(config) { 
       return config; 
      } 
     }; 
     return requestInterceptor; 
    }); 

    app.config(['$httpProvider', function($httpProvider) { 
     $httpProvider.interceptors.push('myInterceptor'); 
    }]); 
+0

感谢您的回复。但是,我需要$ q来进行响应拦截。另外我需要使用打字稿。 – Jhankar

+0

@Jhankar你也可以使用$ q也有 –

+0

如果我使用$ q作为依赖注入,由于使用严格模式,我得到了上述错误。 – Jhankar