【问题标题】:Angular 2 - import of external leaflet typescript libraryAngular 2 - 外部传单打字稿库的导入
【发布时间】:2016-04-02 16:22:43
【问题描述】:

我正在尝试将 typescript 传单库导入到我的 angular 2 应用程序中。

这是我的地图组件。我已经使用 tsd install 安装了 leaflet.d.ts 并且我的应用程序没有抱怨 /// <reference path="../../typings/leaflet/leaflet.d.ts"/> 但是当我尝试使用作为 leaflet.d.ts 中的导出模块的 L.map 时,我收到错误“ReferenceError: L is没有定义的”。这是我第一次尝试在 angular 2 中导入外部打字稿库,显然我做错了什么。

/// <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
         });
    }

package.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\" "
  }
}

tsd.json

{
  "version": "v4",
  "repo": "borisyankov/DefinitelyTyped",
  "ref": "master",
  "path": "typings",
  "bundle": "typings/tsd.d.ts",
  "installed": {
    "leaflet/leaflet.d.ts": {
      "commit": "1da639a106527e0c4010b354a1efe52a3059a291"
    }
  }
}

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

谢谢!

【问题讨论】:

  • 我相信您还需要将leaflet.js 连接到您的页面。 d.ts 文件仅用于告诉 TypeScript 一些 JS 代码,d.ts 不导入实际代码。
  • 好的。我的项目文件夹中有传单 js 文件。你知道我如何将我的 ts 文件连接到这个文件夹吗?
  • 你可以只使用

标签: typescript angular tsd


【解决方案1】:

您需要包含传单 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

【讨论】:

  • 如何在我的 angular-cli 配置文件中添加它?正如你所提到的,我已经添加了系统配置,但是导入传单没有找到该模块。
  • 同上。想要一些关于如何使用 angular-cli 和 webpack 的信息。
【解决方案2】:

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

首先进入项目目录,输入

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

【讨论】:

  • 我猜你想在地图数组中写 'leaflet': 'vendor/leaflet/dist' 对吧?
【解决方案3】:

我必须进行一些更改才能让 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);
    }

【讨论】:

    【解决方案4】:

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

    npm install @types/leaflet@latest --save
    

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

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

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

    另外,地图初始化应该发生在ngOnInit(),构造函数的使用并不意味着逻辑。

    【讨论】:

    • 应该是 --save-dev 吗?
    • @chrismarx 我不知道,应该吗?它不是一个开发工具,而是实际的应用程序代码,所以我猜不是
    • 嗯,类型只对开发有用,类型在编译过程中消失-
    • 使用 Angular CLI,我仍然需要添加 declare var L: any;ng serve 打印 Cannot find name 'L' - 我错过了什么吗?
    猜你喜欢
    • 1970-01-01
    • 2016-10-31
    • 2017-06-21
    • 2017-04-21
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    相关资源
    最近更新 更多