Issue
I am sending data along with the route from my component and I want to retrieve it in the other component's constructor:
Sending data:
this.router.navigate(['/coaches/list'], {
state: { updateMessage: this.processMessage },
replaceUrl: true
});
Retrieving data in the constructor:
if (this.router.getCurrentNavigation().extras?.state != null) {
this.successMessage = this.router.getCurrentNavigation().extras.state;
}
The problem that I am having is that the first time the extras.state is undefined without any data. It is only when I close the dialog button in the other component that this variable will be filled.
Please tell me how can I resolve the issue as I am getting ERROR TypeError: Cannot read property 'extras' of null in the initial loading.
Solution
I was able to resolve the issue by checking, if the getCurrentNavigation is null:
constructor(
private fb: FormBuilder,
private modalService: NgbModal,
private route: ActivatedRoute,
private router: Router,
public commonService: CommonService
) {
if (this.router.getCurrentNavigation() != null) {
this.successMessage = this.router.getCurrentNavigation().extras.state;
}
}
Answered By - Ahmad
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.