简体   繁体   English

"Angular 获取 BehaviorSubject 值错误:“从不”类型上不存在属性“access_token”"

[英]Angular getting BehaviorSubject value error: Property 'access_token' does not exist on type 'never'

In my Angular project I have authentication methods in the auth.service I am storing the admin access token I got from the API in a BehaviorSubject在我的 Angular 项目中,我在 auth.service 中有身份验证方法,我将从 API 获得的管理员访问令牌存储在 BehaviorSubject 中

adminData = new BehaviorSubject(null);

saveAdminData(token:any) {
    let admin:any = new AdminData(token);
    this.adminData.next(admin);
}

You should initialize the BehaviorSubject .您应该初始化BehaviorSubject You have to tell typescript the type of object you are trying to implement, try this:你必须告诉 typescript 你试图实现的对象的类型,试试这个:

adminData = new BehaviorSubject<any | null>(null);

您可以使用行为类似于 BehaviorSubject 的 ReplaySubject,但您不需要初始值。

adminData = new ReplaySubject<any | null>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误TS2339:属性“ access_token”不存在角度2 - error TS2339: Property 'access_token' does not exist angular 2 “从不”类型上不存在属性“id”。 有角度的 - Property 'id' does not exist on type 'never'. angular Angular 类型“从不”上不存在属性“内容” - Angular Property 'content' does not exist on type 'never' 属性&#39;onNext&#39;在&#39;BehaviorSubject类型上不存在 <Filer> “ - Property 'onNext' does not exist on type 'BehaviorSubject<Filer>' “对象”类型上不存在属性“令牌”-Angular 8 - Property 'token' does not exist on type 'Object' - Angular 8 Angular 8 在类型“[]”上不存在类似属性“ASSIGNMENT”的错误 - Angular 8 getting error like Property 'ASSIGNMENT' does not exist on type '[]' 收到错误:“属性&#39;用户&#39;在类型&#39;ArrayBuffer&#39;上不存在。在Angular中 - getting error: "Property 'user' does not exist on type 'ArrayBuffer'. in Angular Angular 7:构建产品错误:类型“组件”上不存在属性“值” - Angular 7 : Build Prod Error: Property 'value' does not exist on type 'Component' 编译错误-angular 8 中的元素类型不存在属性值 - Compilation error-Property value does not exist on type Element in angular 8 错误:类型“存储”上不存在属性“令牌” - Error: Property 'token' does not exist on type 'Storage'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM