错误TypeError:无法读取属性' basemapLayer'未定义的

时间:2017-09-19 15:36:05

标签: angular typescript leaflet angular-cli esri-leaflet

ERROR TypeError:无法读取属性' basemapLayer'未定义的

我使用Angular CLI构建了一个非常基本的应用程序。当我使用ng serve -o构建并运行应用程序时,一切都成功构建。但是,当我在Chrome中查看应用程序时,地图部分无法加载。进一步检查页面我在控制台中看到了这个错误。

ERROR TypeError: Cannot read property 'basemapLayer' of undefined

设置

  • Angular 4
  • Chrome 61
  • 传单1.2.0
  • esri-leaflet 2.1.1
  • 类型/传单1.2
  • 2.1的类型/ esri-leaflet。

重现错误的步骤:

这些步骤假设您已经安装了angular CLI

步骤1-6& 10在终端/ cmd提示窗口中完成

  1. 创建新应用ng new esriLeafletApp
  2. 导航到新应用程序cd esriLeafletApp
  3. npm install --save leaflet
  4. npm install --save esri-leaflet
  5. npm install --save @types/esri-leaflet
  6. npm install --save @types/leaflet
  7. 更新 app.component.ts 文件
  8. 的内容

    
    
    import { Component, OnInit } from '@angular/core';
    
    import * as L from 'leaflet';
    import 'esri-leaflet';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    
    export class AppComponent implements OnInit {
      title = 'app';
    
      constructor() { }
    
      ngOnInit () {
        const map = L.map('map').setView([51.505, -0.09], 13);
        L.esri.basemapLayer('Streets').addTo(map);
      }
    }
    
    
    

    1. 更新 app.component.html 文件的内容
    2. 
      
      <div style="text-align:center">
          <h1>
             Welcome to {{title}}!
          </h1>
      </div>
      <div id="map"></div>
      &#13;
      &#13;
      &#13;

      1. 更新 app.component.css 文件的内容

        &#13;
        &#13;
        #map {
          height: 500px;
          width: 100%;
        }
        &#13;
        &#13;
        &#13;

      2. 构建并运行应用程序ng serve -o

      3. 在Chrome中查看应用程序
      4. 检查代码并在检查器控制台中查看错误
      5. 请帮助

        是否有人知道代码块esriundefinedL.esri.basemapLayer('Streets').addTo(map);的原因以及我如何修复它?

1 个答案:

答案 0 :(得分:0)

似乎问题在于esri-leaflet(@ types / esri-leaflet)的打字文件,而不是esri-leaflet本身。我在DefinitelyTyped github上打开了issue


解决方法:

  • 从package.json和node_modules
  • 中删除ESRI类型
  • 导入esri:import * as esri from 'esri-leaflet';
  • 直接使用esri(即不作为Leaflet的扩展名)。
  • 仍然可以使用传单打字而没有问题。

import { Component, OnInit } from '@angular/core';

import * as L from 'leaflet';
import * as esri from 'esri-leaflet';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit {
  title = 'app';

  constructor() { }

  ngOnInit () {
    const map = L.map('map', {
      maxZoom: 18,
      minZoom: 0
    }).setView([51.505, -0.09], 15);

    const esriLayer = esri.basemapLayer('Imagery');
    map.addLayer(esriLayer);
  }
}