angular - 角度 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;
}
}
解决方案
确保在 ComponentService 上方添加 @Injectable() 装饰器。并且还将 ComponentService 和 CookieService 添加到 providers 数组中。
推荐阅读
- javascript - 如何在 HTML 中收集和解析 JSON 数据
- solr - Solr 可以索引 ASPX 文件吗?
- pandas - 如何在 groupby 计数后创建一个新列?
- terraform - AWS 会话超时后 Terraform 云卡住了
- python - 如何让程序为所有奇数的整数返回 True?
- xcode - 当我使用来自 Firebase 的预构建 UI 在 Xcode 中进行身份验证时给我一个错误。选择器“labelColor”没有已知的类方法
- javascript - 计算其他数组中数组中的特定单词
- python - 如何使用 SimPy 在 Python 中运行并发“任务”,其中每个任务都在等待多个资源?
- pdf - 如何覆盖 Drupal 8 dompdf dpi?
- angular - 是否有任何选项可以延长在 mat-select 中的搜索时间