Reputation: 13
in my project, when I back from page1.ts, I want to trigger an event in page2.ts something like: page1.ts
closeFrom(): void {
//publish an event hear
this.event.publish('someEvent', {name: 'a name'})
history.back();
}
page2.ts
this.event.subscribe('someEvent', (data) => {
const name = data.name;
})
Thank you so much :D
Upvotes: -1
Views: 461
Reputation: 1557
in case there is no relation between those two components - the best practice is to have a Subject
variable inside the service and both components should inject that service - for example:
export MyService {
formClosed = new Subject();
}
first component:
export firstComponent {
constructor(private myService: MyService) {}
closeFrom(): void {
this.myService.formClosed.next(true)
this.event.publish('someEvent', {name: 'a name'})
history.back();
}
}
second component
export secondComponent {
constructor(private myService: MyService) {}
ngOnInit() {
this.myService.formClosed.subscribe(isClosed => {
// do something if it return true
})
}
Upvotes: 2