通过关注这个项目,我设法让 Leaflet 与 Angular 2 和 Webpack 一起工作。
我可以看到在“browser.d.ts”中配置的类型:
/// <reference path="browser\ambient\leaflet\leaflet.d.ts" />
webpack.config.js 定义了一个入口点:
...
entry: {
'polyfills': './src/polyfills.ts',
'libs': './src/libs.ts',
'main': './src/main.ts'
},
...
“libs.ts”包含 Leaflet 模块的导入:
import 'leaflet';
我正在使用 Atom 作为代码编辑器。它现在可以识别所有 Leaflet 类和方法。我现在可以在 Angular 2 中做这样的事情:
import {Map, TileLayer} from 'leaflet';
...
this.baseMaps = {
StreetMap: new TileLayer('mapbox.streets')
};
这是我的问题开始的地方。我正在尝试使用 mapbox.js。我所做的是安装 mapbox.js 库和类型:
npm install mapbox.js --save
typings install mapbox --save
这就是我卡住的地方。对于我的生活,我无法弄清楚如何做 Leaflet 设法做到的事情。
import 'mapbox';
不起作用。
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
我可以看到“browser.d.ts”有以下内容:
/// <reference path="browser\ambient\leaflet\leaflet.d.ts" />
/// <reference path="browser\ambient\mapbox\mapbox.d.ts" />
我想也许 Mapbox 可以正常工作,因为它扩展了 Leaflet 库?
看来我基本上可以做这样的事情,这是标准的javascript方式:
this.baseMaps = {
StreetMap: L.mapbox.tileLayer('mapbox.streets')
};
但不是这个:
this.baseMaps = {
StreetMap: new TileLayer('mapbox.streets')
};
这显然也行不通:
import {Map, TileLayer} from 'mapbox';
我做错了什么?
最佳答案
对于像我这样只想在他们的项目中显示 map 的人来说,我是这样得到它的。主要基于 Angular2 Mapbox-gl Starter .
将 Mapbox-gl 添加到 Angular 2 - Webpack 和 Typescript
安装所需的包...
npm install mapbox-gl --save
npm install @types/mapbox-gl --save
npm install @types/geojson --save
将东西添加到 webpack...
module.exports = function(options) {
return {
resolve: {
alias: {
'mapbox-gl': '../node_modules/mapbox-gl/dist/mapbox-gl.js'
}
},
module: {
postLoaders: [
{
include: /node_modules\/mapbox-gl-shaders/,
loader: 'transform',
query: 'brfs'
}
]
}
}
}
添加一个 map.service.ts(出于某种原因我不得不在导入中使用完整的相对路径)...
import { Injectable } from '@angular/core';
import * as mapboxgl from '../../../../node_modules/mapbox-gl/dist/mapbox-gl.js';
import { Map } from '../../../../node_modules/mapbox-gl/dist/mapbox-gl.js';
@Injectable()
export class MapService {
map: Map<any, any>;
constructor() {
(mapboxgl as any).accessToken = 'YOUR_MAPBOX_TOKEN_HERE';
}
}
将 map 添加到您的组件...
import { Component } from '@angular/core';
import { MapService } from '../../api/resources/map.service';
import { Map } from '../../../../node_modules/mapbox-gl/dist/mapbox-gl.js';
@Component({
selector: 'my-component',
styles: [ require('./my-component.component.less') ],
template: require('./my-component.component.html'),
providers: [ MapService ]
})
export class MyComponentComponent {
constructor(private mapService: MapService) { }
ngOnInit() {
let map = new Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
zoom: 5,
center: [-78.880453, 42.897852]
});
this.mapService.map = map;
}
}
将 map div 添加到您的 html...
// my-component.component.html
<div id="map" class="mapboxgl-map"></div>
对于样式,我使用 LESS
,所以我在那里导入了 mapbox 样式...
// my-component.component.less
@import (less) '../../../../node_modules/mapbox-gl/dist/mapbox-gl.css';
关于typescript - Mapbox typescript ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35931380/