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

z5btuh9x  于 2023-03-31  发布在  TypeScript
关注(0)|答案(4)|浏览(157)

我试图实现一个Map组件与传单和其他传单插件。问题是其他插件不工作从TypeScript出于某种原因。
例如,我无法使用leaflet-draw插件编译代码,并获得错误:
类型“typeof Control”上不存在属性“Draw”
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();
        ...

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"
      }
    }
  ]
...

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"
    ]
  }
cetgtptt

cetgtptt1#

我解决了这个问题,通过导入传单画

import 'leaflet-draw';

不知道为什么它不是通过tsconfig导入的,但是耶,它工作了!

sbtkgmzw

sbtkgmzw2#

谢谢@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);
  });
}
u0sqgete

u0sqgete3#

您应该改为添加此库
npm install --save-dev @types/leaflet-draw
安装这个为我解决了这个问题,并为绘制库提供了类型,而不是让一切都未知。
https://www.npmjs.com/package/@types/leaflet-draw

um6iljoc

um6iljoc4#

npm i --save-dev @types/leaflet-draw

import "leaflet-draw";

相关问题