Angular this.currentUser=currentUser; console.log(this.currentUser);//{ console.log(错误); } ); } 注销(){ this.authenticationService.logout(); this.router.navigate(['../login'],{relativeTo:this.route}); } }

Angular this.currentUser=currentUser; console.log(this.currentUser);//{ console.log(错误); } ); } 注销(){ this.authenticationService.logout(); this.router.navigate(['../login'],{relativeTo:this.route}); } },angular,typescript,rxjs,angular5,Angular,Typescript,Rxjs,Angular5,您正在将一个可观察结果分配给可观察对象本身。这就是你所拥有的: private currentUser=新的可观察对象 然后 this.currentUser = this.authenticationService.getCurrentUser() //<====== here .subscribe( currentUser => { this.currentUser = currentUser; //<======

您正在将一个可观察结果分配给可观察对象本身。这就是你所拥有的:

private currentUser=新的可观察对象

然后

this.currentUser = this.authenticationService.getCurrentUser() //<====== here
      .subscribe(
          currentUser => {
              this.currentUser = currentUser; //<========= and here
          },
          error => {
              console.log(error);
          }
      );
this.currentUser=this.authenticationService.getCurrentUser()//{
this.currentUser=currentUser;//{
console.log(错误);
}
);
而且,console.log将在您收到响应之前运行

试试这个:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

import { Observable } from 'rxjs/Observable';

import { AuthenticationService } from '../_shared/services/authentication.service';

import { CurrentUserInterface } from '../_shared/interfaces/current-user.interface';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {

  private currentUser: CurrentUserInterface;
  private isAuthorized: Observable<boolean> = false;

  constructor(private router: Router,
      private authenticationService: AuthenticationService) { }

  ngOnInit() {
      this.authenticationService.setCurrentUser();
      this.isAuthorized = this.authenticationService.checkAuth()
      .subscribe(
          isAuthorized => {
              this.isAuthorized = isAuthorized;
          },
          error => {
              console.log(error);
          }
      );

      this.authenticationService.getCurrentUser()
      .subscribe(
          currentUser => {
              this.currentUser = currentUser;
              console.log(this.currentUser);  //<====== console.log here
          },
          error => {
              console.log(error);
          }
      );
   }

   logout() {
          this.authenticationService.logout();
          this.router.navigate(['../login'], { relativeTo: this.route });
   }

}
从'@angular/core'导入{Component,OnInit};
从'@angular/Router'导入{Router};
从“rxjs/Observable”导入{Observable};
从“../\u shared/services/authentication.service”导入{AuthenticationService};
从“../\u shared/interfaces/current user.interface”导入{CurrentUserInterface};
@组成部分({
选择器:“应用程序标题”,
templateUrl:'./header.component.html',
样式URL:['./header.component.css']
})
导出类HeaderComponent在Init上实现{
私有currentUser:CurrentUserInterface;
私有未授权:可观察=假;
构造函数(专用路由器:路由器、,
私有authenticationService:authenticationService){}
恩戈尼尼特(){
this.authenticationService.setCurrentUser();
this.isAuthorized=this.authenticationService.checkAuth()
.订阅(
isAuthorized=>{
this.isAuthorized=未授权;
},
错误=>{
console.log(错误);
}
);
this.authenticationService.getCurrentUser()
.订阅(
当前用户=>{
this.currentUser=currentUser;
console.log(this.currentUser);//{
console.log(错误);
}
);
}
注销(){
this.authenticationService.logout();
this.router.navigate(['../login'],{relativeTo:this.route});
}
}

您没有为BehaviorSubject设置初始值为什么不呢?我已经看到了各种具有默认值的示例。在
setCurrentUser
上的
.next
console.log(this.currentUser.value)之后的下一个事件循环之前不会应用
uhh我在控制台日志中得到了一些值。在你的问题中,它说它们是
未定义的
你没有为BehaviorSubject设置初始值为什么不呢?我看到过很多例子,它们都有默认值。在
setCurrentUser
上的
下一个
将在
console.l>之后的下一个事件循环中才会应用og(this.currentUser.value)
uhh我在那个控制台日志上得到了一些值。在你的问题中,它说它们是
未定义的
ngOnInit() {
      this.authenticationService.setCurrentUser();
      // removed assignment to this.isAuthorized
      this.authenticationService.checkAuth()
      .subscribe(
          isAuthorized => {
              this.isAuthorized = isAuthorized;
          },
          error => {
              console.log(error);
          }
      );

      // removed assignment to this.currentUser
      this.authenticationService.getCurrentUser()
      .subscribe(
          currentUser => {
              this.currentUser = currentUser;
              // moved console.log calls into onNext handler. They should not be undefined any more unless the .value properties do really not exist. 
              console.log(this.currentUser.value); 
              console.log(this.isAuthorized.value);
          },
          error => {
              console.log(error);
          }
      );    

   }
this.currentUser = this.authenticationService.getCurrentUser() //<====== here
      .subscribe(
          currentUser => {
              this.currentUser = currentUser; //<========= and here
          },
          error => {
              console.log(error);
          }
      );
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

import { Observable } from 'rxjs/Observable';

import { AuthenticationService } from '../_shared/services/authentication.service';

import { CurrentUserInterface } from '../_shared/interfaces/current-user.interface';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {

  private currentUser: CurrentUserInterface;
  private isAuthorized: Observable<boolean> = false;

  constructor(private router: Router,
      private authenticationService: AuthenticationService) { }

  ngOnInit() {
      this.authenticationService.setCurrentUser();
      this.isAuthorized = this.authenticationService.checkAuth()
      .subscribe(
          isAuthorized => {
              this.isAuthorized = isAuthorized;
          },
          error => {
              console.log(error);
          }
      );

      this.authenticationService.getCurrentUser()
      .subscribe(
          currentUser => {
              this.currentUser = currentUser;
              console.log(this.currentUser);  //<====== console.log here
          },
          error => {
              console.log(error);
          }
      );
   }

   logout() {
          this.authenticationService.logout();
          this.router.navigate(['../login'], { relativeTo: this.route });
   }

}