未捕获的错误:无法解析HttpHeaders的所有参数:(?)

时间:2019-03-16 06:35:43

标签: angular http-headers authorization

get请求中传递标题授权时出错

 compiler.js:486 Uncaught Error: Can't resolve all parameters for HttpHeaders: (?).
    at syntaxError (compiler.js:486)
    at CompileMetadataResolver._getDependenciesMetadata (compiler.js:15706)
    at CompileMetadataResolver._getTypeMetadata (compiler.js:15541)
    at CompileMetadataResolver._getInjectableMetadata (compiler.js:15521)
    at CompileMetadataResolver.getProviderMetadata (compiler.js:15881)
    at eval (compiler.js:15792)
    at Array.forEach (<anonymous>)
    at CompileMetadataResolver._getProvidersMetadata (compiler.js:15752)
    at CompileMetadataResolver.getNgModuleMetadata (compiler.js:15320)
    at JitCompiler._loadModules (compiler.js:34413)

这是我在auth.service.ts中实现并在方法中订阅的代码

getContent() {
    const headerOption = new HttpHeaders ()
    .set('Authorization', 'Bearer ' + btoa(new Util().getData('token')));

    return this.http.get('/path/to/my/api', {headers: headerOption});
  }

这阻止了UI的呈现,请帮帮我!

1 个答案:

答案 0 :(得分:1)

我认为您应该尝试添加方法。如果您查看HttpHeaders api,则会发现以下内容:

append(name: string, value: string|string[]): HttpHeaders { 
    return this.clone({name, value, op: 'a'}); 
}

这意味着您将返回HttpHeaders实例的副本。您可以尝试以下方法:

getContent() {
    const headerOption = new HttpHeaders ();
    headerOption = headerOption.append('Authorization', 'Bearer ' + btoa(new Util().getData('token')))

    return this.http.get('/path/to/my/api', {headers: headerOption});

}

希望这会有所帮助!