Angular 第一次单击Rxjs时,不将值发送到角度方向上的其他组件
我正在尝试使用Angular 2+中的Rxjs将ProductId从一个组件发送到另一个组件。因此,当我第一次单击事件函数时,Rxjs SendFunction没有将ProductId发送给其他组件。但在第二次点击时,它会 这是密码Angular 第一次单击Rxjs时,不将值发送到角度方向上的其他组件,angular,rxjs,Angular,Rxjs,我正在尝试使用Angular 2+中的Rxjs将ProductId从一个组件发送到另一个组件。因此,当我第一次单击事件函数时,Rxjs SendFunction没有将ProductId发送给其他组件。但在第二次点击时,它会 这是密码 Rxjs Class for handling send and get calls import {Subject} from 'rxjs'; @Injectable({ providedIn: 'root' }) export clas
Rxjs Class for handling send and get calls
import {Subject} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class MessengerService {
_Subject = new Subject;
constructor() { }
SendMessageWithData(MessageAndData){
this._Subject.next(MessageAndData);
}
GetMessageWithData(){
return this._Subject.asObservable();
}
}
Component 1.ts
SendProductId(_ProductId){
//Here I am getting the Product id here successfully I cheecked it using console.log
this._MessengerService.SendMessageWithData(_ProductId);
this._Router.navigate(['viewProduct']);
}
Component2.ts
ngOnInit(): void {
//Setting the Local Storage
this._MessengerService.GetMessageWithData().subscribe(docs=>{
console.log(docs);
this._EcommerceDataService.SetLocalStorageForProductId(docs);
});
}
这是我的逻辑代码。但它不适用于第一次点击功能,而是在第一次点击后才起作用。请告诉我最有可能在订阅之前发出的第一个通知。RxJS
Subject
将只向已订阅的观察者发出通知。您可以将ReplaySubject
与缓冲区1一起使用
从'rxjs'导入{ReplaySubject};
出口级信使服务{
_主题=新的ReplaySubject(1);
构造函数(){}
SendMessageWithData(MessageAndData){
this.\u Subject.next(MessageAndData);
}
GetMessageWithData(){
返回此项。_Subject.asObservable();
}
}
兄弟,工作正常。但是,兄弟,我需要了解这件事,你能告诉我任何链接或资源,我可以学习这件事。或者兄弟,你能在这里详细解释一下吗。谢谢兄弟,我能拥有你的linkedin吗?我想和你联系。@AbdulRehman:你可以从这里开始获取资源:。LearnRxJS也是一个很好的起点。虽然我对这个请求很满意,但不幸的是,目前我并没有使用LinkedIn。如果我有空的话,我会在Stackoverflow的个人主页上更新:)