I am building an angular app. I am navigating from first component to second through click event. While navigating to the second component I am wiling to pass some data from first component to the second component. I have followed a tutorial to achieve this but couldn't do it. Below is my code
First component.ts
export class FirstComponent implements OnInit {
@Output() messageEvent = new EventEmitter<string>();
constructor(){
}
ngOnInit() {}
sendMessagetoSecond(ahu) {
this.messageEvent.emit(ahu)
console.log(ahu)
}
first component.html
<map name="ahuMap">
<area shape="rect" coords="818,232,917,262" (click)="sendMessagetoSecond()" [routerLink]="['/secondComponent']">
</map>
second component.ts
ahu : string
receiveMessage($event) {
this.ahu = $event
}
second component .html
<body>
<app-first (messageEvent)="receiveMessage($event)"></app-first>
ahu: {{ahu}}
<div>
....
</div>
</body>
This is what I tried. The problems I am facing are:
1-Variable is not being transferred to the second component
2-HTML of first component is also being loaded in second component.
Is there something that I am missing or doing wrong?
receiveMessagefunction what do you see if youconsole.log($event)?