I have a component that renders leaflet map perfectly and I can draw some polygons on it. I added htmlToImage library to take a screenshot of what I have drawn and if I append it as a child to the same component, it will render fine. But I like to pass that image source to the next component(sibling component) and do other stuff on it. I tried to implement a service that my first component writes the dataURL as a string variable and the second component gets that variable and use it to display image from that stored path. The first component routes to the next one with router-link method as a sibling component (going from home/map to home/image) but it won't show the image and simply the console.log(path) is empty string which is what I initialized in my service, originally. What am I doing wrong?
I am using Angular 9 and running on local host. Here is what I have tried to so far:
map.component.ts
// Map related methods ....
// ....
getScreenshot() {
htmlToImage.toPng(document.getElementById('map'))
.then((dataUrl) => {
this.projectService.setProject('image', dataUrl);
})
.catch((error) => {
console.error('oops, something went wrong!', error);
});
}
screenshot.component.ts
const path = this.projectService.getProject().image;
constructr(private projectService: ProjectService) {}
screenshot.component.html
<div class="row content">
<img [src]="path" alt="map" >
</div>
project.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ProjectService {
private project = {
title: '',
image: '',
};
getProject() {
return this.project;
}
setProject(option: any, value: any) {
this.project[option] = value;
}
}
app-routing.module.ts
export const appRoutes: Routes = [
{
path: 'home', component: AppComponent,
children: [
{ path: 'map', component: MapComponent },
{ path: 'image', component: ScreenshotComponent },
]
},
];
I have posted my code on StackBlitz.