2017-02-22 63 views
0

我不太清楚如何将LRM导入到ts文件中。通过npm install leaflet-routing-machine安装后,我定义路由这样的:如何将Leaflet-routing-machine导入到Ionic2项目中?

var Routing = require('leaflet-routing-machine'); 
var newRoute = Routing.control({Options}); 

这并没有帮助我和我:

Error caused by: Routing.control is not a function 

这里是我的离子信息:

Cordova CLI: 6.4.0 
Ionic Framework Version: 2.0.1 
Ionic CLI Version: 2.2.1 
Ionic App Lib Version: 2.2.0 
Ionic App Scripts Version: 1.1.0 
Node Version: v6.3.1 

BTW,我对传单本身没有任何问题。

回答

0

不知道是否Leaflet Routing Machine插件直接导出自己。

通常它应该至少具有附加到全局名称空间的副作用。

在致电require('leaflet-routing-machine')之后,您是否尝试过使用L.routing.control实例化控件? (注意起步L

+0

是的,我得到这个错误:'属性'路由'不存在类型'typeof L''。 –

1

我们在声明我们的组件之前添加下面的行来解决这个问题。

declare var L: any;

myclass.component.ts

import { Component, OnInit } from '@angular/core'; 
... 

// Leaflet and Leaflet Routing Machine have been installed with npm 
import 'leaflet-routing-machine'; 
import 'leaflet-easybutton'; 

// Add this line to remove typescript errors 
declare var L: any; 

@Component({ 
    ... 
}) 
export class MyClass extends OnInit { 
    ... 

    constructor(...) { 
    ... 
    } 

    ngOnInit() { 

    ... 

    // The example snippet is now working 
    L.Routing.control({ 
     waypoints: [ 
     L.latLng(57.74, 11.94), 
     L.latLng(57.6792, 11.949) 
     ] 
    }).addTo(myMap); 

    ... 

    } 

    ... 

} 

正如this post提到,打字稿似乎导致问题将属性添加到单张的全球大号对象,但在我们的例子声明任何类型的L足以使其工作。