2017-03-06 96 views
1

我試圖實現一個帶有小冊子和其他小冊子插件的地圖組件。問題是由於某些原因,其他插件無法從TypeScript中使用。Property'Draw'在類型'typeof Control'上不存在

例如我無法編譯瓣葉戰平插件代碼,並得到錯誤:

Property 'Draw' does not exist on type '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(); 
     ... 

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

回答

1

我解決了問題,通過導入瓣葉畫

import 'leaflet-draw'; 

不知道爲什麼它沒有被tsconfig進口,但耶它的作品!

+1

這是因爲您正在導入類型定義文件。這不會編譯成實際的JS,但只是一個工具,所以你可以編碼類型安全 – PierreDuc

+0

@PierreDuc哦。任何參考爲進一步閱讀?我想更好地理解它是如何工作的。我爲leaflet-markercluster嘗試了相同的技巧 - 沒有運氣。 – aclokay

+2

@aclokay Google _Angular_和_TypeScript_關係。應該有足夠的材料來了解您擁有的問題。對於初學者:https://github.com/angular/angular-cli/wiki/stories-third-party-lib – Yuri

1

感謝@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); 
    }); 
} 
+0

爲了更好地理解,使用Angular-CLI和傳單庫及其插件的完整示例可以在這裏找到:https://github.com/consbio/Leaflet.ZoomBox/issues/15 –

相關問題