首页 > 解决方案 > 角度 CookieService 实例启动错误

问题描述

我有一个类,我想使用 CookieService 来获取一个值,但我似乎无法初始化 CookieService 我在这里错过了什么?(我是角度新手)

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

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;
  }

}

标签: angularcookieshttp-headers

解决方案


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


推荐阅读