Angular 2 - 导入外部传单打字稿库

时间:2016-04-02 16:22:43

标签: typescript angular tsd

我试图将一个打字稿传单库导入我的angular 2应用程序。

这是我的地图组件。我用tsd install安装了leaflet.d.ts并且我的应用程序没有抱怨master_array[x]但是当我尝试使用在leaflet.d.ts中导出模块的L.map时我收到错误&#34 ; ReferenceError:L未定义"。这是我第一次尝试在角度2中导入外部打字稿库,显然我做错了。

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

package.json

/// <reference path="../../typings/leaflet/leaflet.d.ts"/>
import { Component } from 'angular2/core';
@Component({
  selector: 'map',
  template: `
        <div id="map"></div>
  `,
})
export class Map{
    constructor(){
          var map = new L.Map('map', {
             zoomControl: false
         });
    }

tsd.json

{
  "dependencies": {
    "angular2": "^2.0.0-beta.3",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "normalize.css": "^3.0.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "systemjs": "0.19.6",
    "typings": "^0.6.4",
    "zone.js": "^0.5.11"
  },
  "devDependencies": {
    "concurrently": "^1.0.0",
    "gh-pages": "^0.11.0",
    "grunt": "~0.4.5",
    "grunt-contrib-clean": "^1.0.0",
    "grunt-contrib-copy": "^1.0.0",
    "grunt-contrib-cssmin": "^1.0.0",
    "grunt-contrib-nodeunit": "~0.4.1",
    "grunt-contrib-sass": "~0.9.0",
    "grunt-contrib-uglify": "~0.5.0",
    "grunt-shell": "^1.2.1",
    "lite-server": "^2.0.1",
    "normalize.css": "^3.0.3",
    "typescript": "^1.7.5"
  },
  "scripts": {
    "publish": "node publish.js",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" "
  }
}

有谁能告诉我我做错了什么?

谢谢!

4 个答案:

答案 0 :(得分:4)

您需要包含传单JS文件:

System.config({
  map: {
    leaflet: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/leaflet.js'
  },
  packages: {'app': {defaultExtension: 'ts'}} 
});
System.import('app/main')
        .then(null, console.error.bind(console));

然后你可以这样在你的模块中导入它:

import {Component, OnInit} from 'angular2/core';
import leaflet from 'leaflet';

请参阅此plunkr:http://plnkr.co/edit/aUo2uvlxC5ji32u01jfF?p=preview

答案 1 :(得分:3)

我可以建议你一个解决方法,直到angular-cli获得对第三方库的更好支持。它对我有用:)

首先转到项目目录并输入

npm install leaflet --save

然后打开你的angular-cli-build.js并添加这一行

vendorNpmFiles: [
   ..................
   'leaflet/**/*.js',
    ....................
  ]

现在打开你的src / system-config.ts,写

const map:any ={
     ..................
   'leaflet': 'vendor/leaflet/dist',
    ....................

}

const packages: any = {
  'leaflet': {
    format: 'cjs'
  }

};

打开你的src / index.html,添加这个

 <script type="text/javascript" src="vendor/leaflet/dist/leaflet.js"></script>

并且不要忘记也添加css文件

  <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />

现在打开要使用Leaflet的组件

declare let L: any;
@Component({
  moduleId: module.id,
  selector: 'app-map',
  templateUrl: 'map.component.html',
  styleUrls: ['map.component.css']
})
export class MapComponent implements OnInit, AfterViewInit {
 leafletMap: any;
 ngAfterViewInit() {
    this.leafletMap = L.map("map").setView([23.709921, 90.407143], 7);
 }
}

多田!!您的地图已加载:D

答案 2 :(得分:2)

同时,everything you have to do正在调用

npm install @types/leaflet@latest --save

并在index.html中包含源代码。

不需要其他进口或声明。然后,导入可能看起来像

import {control, LatLng, layerGroup, LayerGroup, Map} from "leaflet";

此外,地图初始化应该在ngOnInit()中进行,构造函数的使用不适用于逻辑。

答案 3 :(得分:2)

我必须做一些修改才能让Thierry Templier使用systemjs和Angular 2.4来解决问题。

我将Leaflet添加到我的system.config.js文件中,就像他说的那样,但在我的组件中我必须以这种方式导入它:

import * as L from 'leaflet';

然后在我的MapComponent中识别出L类

export class MapComponent {
    let map = L.map("map").setView([38, -77], 13);
            L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
            attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
            }).addTo(map);
    }