Basically I'm trying to navigate between two view (let's call them view1 and view2) models using Router.navigate function. I need to pass some data from view1 to view2 during navigation and for that I'm using event Aurelia's event aggregator like described bellow. Also, view2 has a ref item I need to access inside the subscription event.
View1
...
this.router.navigate("view2").then(() => {
console.log("Publishing message");
this.eventAggregator.publish(new ShareDataMessage(dataToShare));
});
View2
...
attached() {
console.log("View attached");
console.log("RefItem 1: " + this.refItem);
this.eventAggregator.subscribeOnce(ShareDataMessage, message => {
console.log("Event received")
console.log("RefItem 2: " + this.refItem);
});
}
However, if I run the app, and navigate from view1 to view2, i get the following output:
Publishing message
Event received
RefItem 2: null
View attached
RefItem 1: [object HTMLImageElement]
Obviously this was not the execution flow I expected. Can someone please explain me what I'm missing?