我正在尝试让 Leaflet 和插件在 Ionic 2 项目中协同工作。我已经安装并导入了 Leaflet 本身以及传单路径变换 https://github.com/w8r/Leaflet.Path.Transform插件,以及Leaflet类型声明 https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/leaflet-editable包含在 DefinatelyTyped 中。如果我添加,代码就可以正常工作declare var L: any;
忽略类型声明。
这是页面控制器(home.ts)的重要部分:
var polyline = new L.Polyline(
L.GeoJSON.coordsToLatLngs([
[114.14314270019531, 22.49479484975443],
[114.21798706054688, 22.524608511026262],
[114.20768737792969, 22.524608511026262],
[114.20768737792969, 22.536024805886974]
]), {
weight: 15,
draggable: true,
transform: true
})
.bindPopup("L.Polyline")
.addTo(map);
polyline.transform.enable();
draggable
and transform
不是基本传单中的有效选项(也不是.transform
),所以我写了一个类型声明文件PROJECT_DIR\node_modules\@types\leaflet-path-transform\index.d.ts
来定义它们。到目前为止我所写的内容是基于leaflet-editable 的类型声明 https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/leaflet-editable,因为该插件允许在创建 L.Map 对象时使用新的 MapOptions。
/// <reference types="leaflet" />
declare namespace L {
export interface Polyline {
draggable: boolean;
}
namespace Polyline {
export interface PolylineOptions {
draggable: boolean;
}
}
}
到目前为止,这仅解决了第一个选项,但尚未成功清除 TypeScript 错误:
Typescript Error
Argument of type '{ weight: number; draggable: boolean; }' is not assignable to parameter of type 'PolylineOptions'. Object literal may only specify known properties, and 'draggable' does not exist in type 'PolylineOptions'.
有人可以帮我弄清楚我在这里做错了什么吗?起初我以为新的类型声明文件只是被忽略了,但如果我在其中留下拼写错误,它会触发新的错误,所以它似乎确实有效果。
如果有帮助的话,这是我工作的环境:
- 离子框架:2.0.0
- 离子本机:2.4.1
- 离子应用程序脚本:1.0.0
- 角度核心:2.2.1
- Angular 编译器 CLI:2.2.1
- 节点:6.9.4
- 操作系统平台:Windows 10
- 导航器平台:Win32
- 用户代理:Mozilla/5.0(Windows NT 10.0;WOW64)AppleWebKit/537.36(KHTML,如 Gecko)Chrome/56.0.2924.87 Safari/537.36