この GitHub リポジトリを確認してください https://github.com/nileshzala005/Service-demo
userServiceルート レベルで登録されます。
のにgetUserLogin観測可能な値を取得したいと思います。ユーザーがボタンをクリックすると、で宣言されているからユーザー コンポーネントに値を送信します。SharedModuleUserComponentHomeComponentHomeModule
UserServiceルート レベルで:
userLogin = new Subject<string>();
constructor() { }
sendUserLogin(user: string) {
console.log("value received at user service ", user);
this.userLogin.next(user);
}
getUserLogin(): Observable<string> {
return this.userLogin.asObservable();
}
HomeComponentでHomeModule:
export class HomeComponent implements OnInit {
constructor(private userService: UserService) {
}
ngOnInit() {
}
onClick() {
console.log("value is send from home component", "Nilesh")
this.userService.sendUserLogin("Nilesh");
}
}
UserComponentでSharedModule:
export class UserComponent implements OnInit {
constructor(private userService: UserService) { }
ngOnInit() {
this.userService.getUserLogin().subscribe(res => {
/// here it should receive but not able to get it
console.log("user component should get value here ", res);
});
}
}