Answer the question
In order to leave comments, you need to log in
How to wait for library initialization?
The question is how to make it so that all child components wait for the service to be initialized - which will return the map object.
The service is initialized in the parent component app.component and has a reference to the DOM element.
So, the rest of the components also use this service to get access to the map - but do not wait for it to be fully initialized.
Main Component:
import { Component } from "@angular/core";
import { ReonMapLibraryService } from "../../projects/reon-map-library/src/lib/reon-map-library.service";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.scss"]
})
export class AppComponent {
constructor(private ReonMapLibraryService: ReonMapLibraryService) {}
ready() {
return this.ReonMapLibraryService.ready();
}
}
<lib-reon-map-library></lib-reon-map-library>
<app-menu></app-menu>
<lib-reon-map-library></lib-reon-map-library>
is the child component where the service initialization takes place:@Component({
selector: "lib-reon-map-library",
template: `
<div id="map-container" #mapContainer></div>
`,
styles: []
})
export class ReonMapLibraryComponent implements AfterViewInit {
@ViewChild("mapContainer", { static: true }) map: ElementRef;
constructor(private ReonMapLibraryService: ReonMapLibraryService) {}
ngAfterViewInit(): void {
this.ReonMapLibraryService.initialization(this.map.nativeElement);
}
}
<app-menu></app-menu>
<app-menu2></app-menu2>
<lib-reon-map-library></lib-reon-map-library>:
<lib-reon-map-library></lib-reon-map-library>
<app-menu></app-menu>
<app-menu2></app-menu2>
constructor(private ReonMapLibraryService: ReonMapLibraryService) {}
Answer the question
In order to leave comments, you need to log in
If the service is not immediately ready to work, a ready $ stream should stick out of it. Which can be attached to ngIf on a component is not an example.
In general, it is not clear from the code why something falls.
The dependency should not lead to a crash, the injector looks for or creates its dependencies before creating an instance, and the problem is not in your component, but in the service.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question