在JavaScript(含傳單),書面文檔中我可以實現使用L.Class.include()
一些自定義functionnalities等效L.Class.include()的:與打字稿
包括與L-現有類更多的功能.Class.include() 添加新的方法和選項 更改某些方法 使用addInitHook運行額外的構造函數代碼。
這工作:
L.Rectangle.include({
contains: function (markers: L.Marker[]) {
const markersContained: boolean[] = [];
markers.forEach(marker => {
console.log(marker);
markersContained.push(this.getBounds().contains(marker.getLatLng()));
})
return markersContained;
}
});
現在我想要實現相同與打字稿的Angular4應用程序,但include()
功能似乎並沒有在@types/leaflet
問題是我的代碼是在.ts
文件中爲Leaflet.Draw
插件量身定做的,所以我不得不用這種方法初始化文件:
declare const L: any;
import 'leaflet-draw/dist/leaflet.draw-src';
,而不是常規
import * as L from 'leaflet';
由於該Leaflet.Draw
插件有沒有@types
,因此我無法導入正規傳單類型的事實(否則,new L.Control.Draw()
將使編譯錯誤)。
這打破在編譯的時候:
map.on(L.Draw.Event.CREATED, (geometry: L.LayerEvent) => {
const layer = geometry.layer;
drawnItems.addLayer(layer);
// Set an array containing all the markers
const markers = LeafletService.prototype.jsonToArray(layerGroup.getLayers());
// ===> Property 'contains' does not exist on type 'Layer'.
const result = geometry.layer.contains(markers);
console.log('result => ', result);
});
,顯然,這個作品,如果我把我的幾何類型爲any
,但我想如果可能的話用L.LayerEvent
類型,並想知道我如何使用TypeScript語法正確使用Leaflet include()
方法?