如何将小叶路由机包含到angular 2打字稿webpack应用程序中 [英] How to include leaflet-routing-machine into angular 2 typescript webpack application

查看:78
本文介绍了如何将小叶路由机包含到angular 2打字稿webpack应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此种子开始使用Angular 2 Typescript和Webpack: https://github .com/haoliangyu/angular2-leaflet-starter .

I used this seed to get started with Angular 2 Typescript and Webpack: https://github.com/haoliangyu/angular2-leaflet-starter.

我是大多数二手工具和技术(Angular 2,Typescript,Webpack)的新手.尽管我对这些知识越来越了解,但似乎我仍然不了解如何包含第三方非类型化JS库:

I'm new to most of the used tools and technologies (Angular 2, Typescript, Webpack). While I understand more and more about these it seems that I still haven't grasped how third-party non-typed JS-libraries are included:

我想将 leaflet-routing-machine.js 包含在我的计算机中项目.据我所知,虽然确实存在传单的类型输入,但不存在传单路由机的类型.

I would like to include the leaflet-routing-machine.js into my project. To my knowledge, while there do exist typings for leaflet, there do not exist typings for leaflet-routing-machine.

我通过npm install安装了该软件包,并添加了必需的快速删除起始代码以显示路线.

I installed the package via npm install and added the required quick-start code snipped to display a route.

map.service.ts

/// <reference path="../../typings/leaflet/leaflet.d.ts"/>

import {Injectable} from 'angular2/core';
import {Map} from 'leaflet';

Injectable()
export class MapService {
  map: Map; // holds reference to the normal leaflet map object

  showRoute(){
        L.Routing.control({
        waypoints: [
            L.latLng(47.569198, 7.5874886),
            L.latLng(47.5685418, 7.5886755)
        ]
    }).addTo(this.map);

  }

}

我在npm start上遇到的错误是:

ERROR in ./app/services/map.service.ts
(56,11): error TS2339: Property 'Routing' does not exist on type 'typeof L'.

据我了解,我不应该在index.html中包含JS文件,因为这应该由webpack自动完成.除了我通常不确定如何在不键入内容的情况下包含第三方库这一事实之外,(例如

As I understand it I shouldn't include the JS file in the index.html as this should be automatically done by webpack. Next to the fact that I'm generally unsure how to include third-party libraries without typings (answers like this did not help) it seems my case is a bit different because the L Object is standard leaflet and does not know the Routing property. Which I somehow understand, since I don't see how the routing machine library extends the leaflet library.

有什么建议吗?

推荐答案

我既没有使用Angular 2,也没有使用TypeScript,但是我怀疑TypeScript不喜欢LRM将自身附加到L对象/命名空间.

I haven't worked with either Angular 2, nor TypeScript, but I suspect that TypeScript doesn't like that LRM attaches itself to the L object/namespace.

请注意,LRM还会将自身导出为普通的CommonJS模块,因此您可以执行以下操作,而不是使用L.Routing.Control:

Note that LRM also exports itself as a normal CommonJS module, so you can do something like this instead of using L.Routing.Control:

var L = require('leaflet');
var Routing = require('leaflet-routing-machine');

var map = L.map(...);
var routingControl = Routing.control({ ... });

这篇关于如何将小叶路由机包含到angular 2打字稿webpack应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆