This is the service class for managing data
export class DataService {
public confirmationStatus = new Subject();
updateConfirmationStatus(status: boolean) {
this.confirmationStatus.next(status);
}
getConfirmationStatus(): Observable<any> {
return this.confirmationStatus.asObservable();
}
}
This is my first component
constructor(
private dataService: DataService
)
confirmationStatus: boolean = true;
leave() {
this.dataService.updateConfirmationStatus(this.Confirm);
}
This is another component where leave()
is triggered by a click event in html.
confirmation: boolean;
checkExit(): boolean{
subscription: Subscription;
this.subscription = this.dataService.getConfirmationStatus().subscribe((status) => {
console.log(status);
this.confirmation = status;
console.log(this.confirmation);
});
}
This is the route guard implementation
export class DeactivateGuardService implements CanDeactivate<SolutionCanvasComponent> {
component: Object;
route: ActivatedRouteSnapshot;
constructor() { }
canDeactivate(component: Component2,
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot,
nextState?: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return component.checkExit();
}
I'm facing challenges in passing data from the first component to the second component. Should I include the service in the providers array of any module?