angular CookieService实例启动错误

时间:2018-05-31 06:15:06

标签: angular cookies http-headers

我有一个类,我想使用CookieService来获取值,但我似乎无法初始化CookieService,我在这里缺少什么? (我是棱角分明的新人)

我收到“无法读取属性'获取'未定义”错误

import {HttpHeaders} from '@angular/common/http';
import {HttpErrorResponse} from '@angular/common/http';
import {ErrorObservable} from 'rxjs/observable/ErrorObservable';
import { CookieService } from 'ngx-cookie-service';

export abstract class ComponentService {

  constructor(private cookieService: CookieService) { }

  baseURL: String = 'http://localhost:8080';

  public getHttpOptions(){
    let auth:String = this.cookieService.get('Authorization');
    let headers: HttpHeaders = new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': String(auth)
    });
    return headers;
  }

}

1 个答案:

答案 0 :(得分:1)

确保在ComponentService上方添加@Injectable()装饰器。并且还将ComponentService和CookieService添加到providers数组中。