简体   繁体   English

Angular 可观察和订阅

[英]Angular observable and subscription

i have one observable function in my service which i have subscribe but i am getting [object Object] instead of token value我的服务中有一个可观察的函数,我已经订阅了,但我得到的是[object Object]而不是令牌值

service code:服务代码:

get bearerToken(): Observable<string | null> {
  return this.store.select(AuthStatusState.getToken).pipe(
       map((token) => {
         if (token) {
           return token;
         } else {
           return null;
         }
       }),
       take(1)
     );
   }

code to which i have subscribed.我订阅的代码。

const authToken = this.authService.bearerToken.subscribe();
console.log(authToken);

i want token to be saved in const instead of object我希望将令牌保存在 const 而不是 object

Like this authToken is a subscription.像这个authToken是一个订阅。

const authToken = this.authService.bearerToken.subscribe();

Like this authToken will be the value emitted by bearerToken .像这个authToken将是bearerToken发出的值。

let authToken;
this.authService.bearerToken.subscribe( data => authToken = data);
console.log(authToken);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM