属性“ Draw”在类型“ typeof Control”上不存在

acl*_*kay 5 leaflet typescript leaflet.draw angular-cli angular

我正在尝试使用传单和其他传单插件来实现地图组件。问题是由于某种原因,其他插件无法从TypeScript使用。

例如,我无法使用Leaflet-Draw插件编译代码并得到以下错误:

属性“ Draw”在类型“ typeof Control”上不存在

mapbox.component.ts

import { DataService } from "../data-service.service";
import { Component, OnInit } from '@angular/core';


import * as $ from 'jquery';
/// <reference types="leaflet" />
/// <reference types="leaflet-draw" />

declare var require: any


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

export class MapboxComponent implements OnInit {

    constructor(private dataService: DataService) { }
    // helper flags
    map: L.Map = null;
    aggreagte: boolean = false;

    ngOnInit() {
        // Prepare map
        this.map = L.map('resultmap').setView([51.505, -0.09], 1);
        //
        L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
            attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
            maxZoom: 18,
            id: 'mapbox.streets',
            accessToken: '...'
        }).addTo(this.map);

        var drawnItems = L.featureGroup();
        this.map.addLayer(drawnItems);
        var control = new L.Control.Draw();
        ...
Run Code Online (Sandbox Code Playgroud)

angular-cli.json

"apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "styles": [
        "styles.css",
        "../node_modules/leaflet/dist/leaflet.css",
        "../node_modules/leaflet-markercluster/MarkerCluster.css",
        "../node_modules/leaflet-draw/dist/leaflet.draw.css"
      ],
      "scripts": [
        "../node_modules/jquery/dist/jquery.min.js",
        "../node_modules/leaflet/dist/leaflet.js",
        "../node_modules/leaflet-markercluster/leaflet.markercluster.js",
        "../node_modules/leaflet-draw/dist/leaflet.draw.js",
        "../node_modules/chart.js/dist/Chart.bundle.min.js"
      ],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ]
...
Run Code Online (Sandbox Code Playgroud)

tsconfig.json

"compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc-e2e",
    "sourceMap": true,
    "target": "es5",
    "files":[
      "../node_modules/@types/leaflet-draw/index.d.ts"
    ],
    "typeRoots": [
      "../node_modules/@types"
    ],
    "types":[
      "jquery",
      "leaflet",
      "leaflet-draw",
      "leaflet-markercluster"
    ]
  }
Run Code Online (Sandbox Code Playgroud)

acl*_*kay 6

我通过导入传单图纸解决了这个问题

import 'leaflet-draw';
Run Code Online (Sandbox Code Playgroud)

不确定为什么tsconfig不导入它,但是可以!

  • @aclokay Google _Angular_和_TypeScript_关系。应该有足够的材料供您理解所遇到的问题。对于初学者:https://github.com/angular/angular-cli/wiki/stories-third-party-lib (2认同)
  • 我有完全相同的示例,但仍然无法正常工作。我收到 L.Control.Draw 不是构造函数,所以它没有得到打字稿定义,我不知道为什么。tsc -v 版本 2.2.1 ng -v @angular/cli: 1.0.0-rc.0 node: 6.2.0 os: linux x64 我不知道为什么,也不知道为什么传单标记集群。你能分享你的代码或放一个完整的例子吗? (2认同)

Ale*_*net 5

感谢@aclokay 的洞察力。我会通过补充说您不要忘记更改标准传单导入来完成这个答案。例如 :

// import * as L from 'leaflet';  
// --> Doesn't work : Property 'Draw' does not exist on type 'typeof Control'.
declare const L: any; // --> Works
import 'leaflet-draw';

export function drawPlugin(map: any) {
  const drawnItems = L.featureGroup().addTo(map);

  const drawControl = new L.Control.Draw({
    edit: {
      featureGroup: drawnItems,
    },
    draw: {
      polygon: false,
      polyline: false,
      marker: false,
      circlemarker: false
    }
  });
  map.addControl(drawControl);

  map.on(L.Draw.Event.CREATED, function (event) {
    const layer = event.layer;

    drawnItems.addLayer(layer);
  });
}
Run Code Online (Sandbox Code Playgroud)