0

User.component.ts

 user:User;

  constructor(private authService:AuthService) {}

  ngOnInit(): void {
    this.authService.user.subscribe((userVal) => {
      this.user = userVal;
      console.log(this.user);
    });
  }

User.component.html

<div class="container mt-3 ">
    <app-user-detail [userData]="user"></app-user-detail>
</div>

User.detail.component.ts

export class UserDetailComponent implements OnChanges {
  @Input() userData: User;

  constructor() {}

  ngOnChanges() {
    console.log(this.userData)
  }

}

Getting the value of "this.user" in User.component.ts but getting undefined in User.detail.component.ts

Why is that?

3
  • This is answered here stackoverflow.com/questions/49662341/… Commented Jul 28, 2021 at 19:00
  • Answer no 1. of top answer doesnt work which is what I want to work, ngOnChanges(changes) are undefined " userData: SimpleChange currentValue: null firstChange: true previousValue: undefined" Answer no 2. obviosly works since its a duplication of the code in the parent but that would be ugly id rather not use that Commented Jul 29, 2021 at 8:50
  • You may have add the service code as well. I believe you need to have BehaviorSubject. A sample stackblitz here - stackblitz.com/edit/… Commented Jul 29, 2021 at 14:58

1 Answer 1

1

Try this:

[userData]="user | async"

and if it alone doesn't work, add these changes:

get user(){
return this.authService.user;
}

constructor(private authService:AuthService) {}

  ngOnInit(): void {
}
<div class="container mt-3 ">
    <app-user-detail [userData]="user | async"></app-user-detail>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't work, still getting the same undefined in UserDetailComponent either way. Btw This.authService.user is a Subject in authService

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.