Enjoying exploring Aurelia 2. When porting an app from 1 to 2 (this app) - ran into early difficulties attempting to initialise at third party component (leaflet maps). Here is a minimal Aurelia 1 example that works fine:
map: LeafletMap;
afterAttach() {
this.map = new LeafletMap(this.mapDescriptor);
}
However, the above yields exceptions:
leaflet-src.js:4115 Uncaught Error: Map container not found.
at NewClass._initContainer (leaflet-src.js:4115)
at NewClass.initialize (leaflet-src.js:3149)
at new NewClass (leaflet-src.js:301)
at Object.createMap [as map] (leaflet-src.js:4740)
at new LeafletMap (leaflet-map.ts:18)
at MyApp.afterAttach (my-app.ts:23)
at Controller.afterAttach (controller.js:317)
at AttachedQueue.process (lifecycle.js:231)
at AttachedQueue.end (lifecycle.js:190)
at Controller.attachCustomElement (controller.js:580)
I have tried all of the other lifecycle methods to no effect.
The element in the view is defined like this in both applications:
I also tried various combinations of a hard coded id in the view to no effect. It seems that the element is not available on the DOM when the leaflet component attempts to create itself?
Any help appreciated - and I understand if this is still too early in Au2 to be a valid application.
I think I am doing more or less the same as you in my leaflet wrapper - but your example might be Aurelia 1 I think? Have you tried Leaflet in Aurelia 2 (using afterAttach instead)? This is where I am having trouble.
Thanks
I just created a dumber gist from my custom element and it seems to init just fine.
The map doesnât show because I didnât add in the tile server stuff.
While a leaflet map from the âmainâ page is successfully created - I am not having much luck replicating same in a âcomponentâ - particularly a component triggered by the new au2 router. This is illustrated here:
The map displays fine in the my-app component. However, the identical code in routes/home fails.
This is currently commented out in routes/home.ts:
export class Home implements IViewModel {
...
map: LeafletMap;
afterAttach() {
//this.map = new LeafletMap(this.mapDescriptor);
}
But when included, container not found exception thrown from leaflet
Uncaught (in promise) Error: Map container not found.
at NewClass._initContainer (entry-bundle.js:39606)
at NewClass.initialize (entry-bundle.js:38640)
...
It appears to me that the document object leaflet is using is the global document before Aurelia 2 does its magic of composing.
When I trace through the Leaflet initialization to the point where it is doing a document.getElementById() everything is correct up to that point (i.e. the map id passed is available and correct). However, the document is just the raw index.html as viewed under document.body.innerHtml.