类型“对象”上不存在属性“令牌”

时间:2020-05-22 22:34:28

标签: angular spring-boot spring-mvc angular8

src / app / login / login.component.ts:21:46中的错误-错误TS2339:类型“对象”上不存在属性“令牌”。

21 localStorage.setItem(“ xAuthToken”,res.token);

login.service.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';

export class LoginService {

  constructor(private http:HttpClient) { }

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

login.component.ts

onSubmit(){
    this.loginService.sendCredential(this.credential.username, this.credential.password).subscribe(
      res => {
          console.log(res);
      localStorage.setItem("xAuthToken", res.token);
          this.loggedIn = true;
          location.reload();
      },
      error => {
          console.log(error);
      }
  );
  }

0 个答案:

没有答案