I managed to get Leaflet working with Angular 2 and Webpack by following this project.
I can see the typings configured in "browser.d.ts":
/// <reference path="browser\ambient\leaflet\leaflet.d.ts" />
webpack.config.js defines an entry point:
...
entry: {
'polyfills': './src/polyfills.ts',
'libs': './src/libs.ts',
'main': './src/main.ts'
},
...
The "libs.ts" contains the import of the Leaflet module:
import 'leaflet';
I'm using Atom as a code editor. It now recognises all the Leaflet classes and methods. I can now do things like this in Angular 2:
import {Map, TileLayer} from 'leaflet';
...
this.baseMaps = {
StreetMap: new TileLayer('mapbox.streets')
};
Here is where my problems start. I'm trying to use mapbox.js. What I did was I installed mapbox.js library and typings:
npm install mapbox.js --save
typings install mapbox --save
This is where I'm stuck. For the life of me I can't figure out how to do what Leaflet managed to do.
import 'mapbox';
Doesn't work.
ERROR in ./src/libs.ts
Module not found: Error: Cannot resolve module 'mapbox' in C:\Develop\angular2-webpack-starter\src
@ ./src/libs.ts 3:0-17
I can see "browser.d.ts" has the following:
/// <reference path="browser\ambient\leaflet\leaflet.d.ts" />
/// <reference path="browser\ambient\mapbox\mapbox.d.ts" />
I thought maybe Mapbox will just work, because it extends the Leaflet library?
It seems that I can basically do something like this, which is the standard javascript way:
this.baseMaps = {
StreetMap: L.mapbox.tileLayer('mapbox.streets')
};
But not this:
this.baseMaps = {
StreetMap: new TileLayer('mapbox.streets')
};
This obviously doesn't work either:
import {Map, TileLayer} from 'mapbox';
What am I doing wrong?
For anyone like me who just wanted to get a map to show in their project, here's how I got it. Based mostly on Angular2 Mapbox-gl Starter.
Adding Mapbox-gl to Angular 2 - Webpack and Typescript
Install the needed packages...
npm install mapbox-gl --save
npm install @types/mapbox-gl --save
npm install @types/geojson --save
Add stuff to webpack...
Add a map.service.ts (For some reason I had to use full relative paths in the imports)...
Add maps to your component...
Add map div to your html...
For styles, I use
LESS
, so I imported the mapbox styles there...You can just use the
mapbox.js
NPM module, and it'll include everything you need.npm install mapbox.js --save
See this example. We're using Webpack to do the module loading, and with TypeScript you need to explicitly
import *
for untyped modules.