启动了 angular-cli 项目并通过 npm 安装了 mapbox-gl。在 angular-cli.json 文件中包含了 mapbox-gl 脚本和样式。我可以显示地图,但无法在加载事件上添加图层。地图突然不定义了?这是模板 html (app.component.html):
<div id="map" style="margin:0; padding:0; position:absolute; top:25px; bottom:0; width:100%;">
</div>Run Code Online (Sandbox Code Playgroud)
这是我的组件代码(app.component.ts):
import { Component, ViewChild, AfterViewInit, OnInit } from '@angular/core';
import * as mapboxgl from 'mapbox-gl';
import { Map } from 'mapbox-gl';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild("map") map: Map;
title = 'app';
ngOnInit() {
mapboxgl.accessToken = 'blah';
this.map = new Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
zoom: 5,
center: [-78.880453, …Run Code Online (Sandbox Code Playgroud)