Spring Boot的基本标头-未捕获的错误:无法解析HttpHeaders的所有参数:(?)

时间:2019-05-17 17:12:37

标签: angular

我已将代码从Angular 2升级到了Angular6。http标头中设置的参数为content-typeauthorizationauthorization包含btoa的用户名和密码。

我找不到设置httpheaders参数的错误。

这是从Spring启动授权的。附加了appmoduleservice的代码。

  

应用模块

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule,HttpHeaders } from '@angular/common/http';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MenuComponent } from './components/menu/menu.component';
import { LoginComponent } from './components/login/login.component';
import { AngularMaterialModule } from './angular-material.module';
import { AppRoutingModule } from './/app-routing.module';
import { FormsModule } from '@angular/forms';

@NgModule({
    declarations: [
        AppComponent,
        MenuComponent,
        LoginComponent
    ],
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        FormsModule,
        HttpClientModule,
        AngularMaterialModule,
        AppRoutingModule
    ],
    providers: [HttpHeaders],
    bootstrap: [AppComponent]
})
export class AppModule { }

  

登录服务

import { Injectable } from '@angular/core';
import {  HttpClient, HttpHeaders} from '@angular/common/http';
import { Token } from '../models/Token';

@Injectable({
    providedIn: 'root'
})
export class LoginService {
    constructor(private http: HttpClient,private header: HttpHeaders) { }

    sendCredential(username: string, password: string) {
        let url = 'http://localhost:8080/token';
        let headers = new HttpHeaders({
            'Content-Type' : 'application/x-www-form-urlencoded',
            'Authorization' : 'Basic ' + btoa(username+":"+password)
        });
        return this.http.get<Token>(url, { headers });
    }

    checkSession() {
        let url = 'http://localhost:8080/checkSession';
        let headers = new HttpHeaders({
            'x-auth-token' : localStorage.getItem('xAuthToken')
        });
        return this.http.get(url, { headers });
     }

    logout() {}
}

2 个答案:

答案 0 :(得分:1)

在登录服务中

constructor(private http: HttpClient) { }

答案 1 :(得分:0)

感谢您的答复。

通过指定以下语句解决了该问题:app模块中的HttpHeaders。

声明是

提供者:[{provide:HttpHeaders}]