可观察和订阅

发布于 2025-01-28 17:12:40 字数 537 浏览 1 评论 0原文

我的服务中有一个可观察到的功能,我已经订阅了它,但是我得到 [对象] 而不是令牌值

服务代码:

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

我已订阅的代码。

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

我想将令牌保存在const而不是对象中

i have one observable function in my service which i have subscribe but i am getting [object Object] instead of token value

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九命猫 2025-02-04 17:12:40

像这样的authtoken是订阅。

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

像这样的authtoken将是BearerToken发出的值。

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

Like this authToken is a subscription.

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

Like this authToken will be the value emitted by bearerToken.

let authToken;
this.authService.bearerToken.subscribe( data => authToken = data);
console.log(authToken);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文