请查看此 GitHub 存储库https://github.com/nileshzala005/Service-demo
userService
在根级别注册。
我想在's中获得getUserLogin
可观察的价值。当用户单击按钮时,将值从.SharedModule
UserComponent
HomeComponent
HomeModule
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);
});
}
}