我试图让Leaflet和插件在Ionic 2项目中一起工作。我已经安装并导入了Leaflet本身和leaflet-path-transform插件,以及包含在DefinatelyTyped中的the Leaflet type declaration。如果我添加declare var L: any;
以忽略类型声明,代码全部有效。类型X的参数不能分配给类型为Y的参数。对象常量只能指定已知属性,而类型Y中不存在X
这是页面控制器的重要组成部分(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
和transform
是基本的宣传单不是有效的选项(也不是.transform
),所以我写了一个类型声明文件在PROJECT_DIR\node_modules\@types\leaflet-path-transform\index.d.ts
来定义它们。到目前为止我写的是基于the type declaration for leaflet-editable,因为该插件允许在创建L.Map对象时使用新的MapOptions。
/// <reference types="leaflet" />
declare namespace L {
export interface Polyline {
draggable: boolean;
}
namespace Polyline {
export interface PolylineOptions {
draggable: boolean;
}
}
}
这只能满足第一个选项,到目前为止,但它并没有成功地清除打字稿错误:
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
- 角编译CLI:2.2.1
- 节点:6.9.4
- 操作系统:Windows 10
- 导航器平台:Win32
- 用户代理:Mozilla/5.0(Windows NT 10.0; WOW64)为AppleWebKit/537.36(KHTML,例如Gecko)Chrome浏览器/ Safari浏览器56.0.2924.87/537.36
你得到它的工作? – ackuser
还没有。我刚刚使用“any”修饰符,而不是正确的类型声明。 – carpiediem