【问题标题】:Importing typescript module into angular component将打字稿模块导入角度组件
【发布时间】:2018-05-01 11:17:50
【问题描述】:

我正在尝试在我的组件中使用某个库,但我似乎缺少正确包含它的步骤

我的组件如下。有问题的图书馆是 geolib

import { Component, OnInit } from '@angular/core';
import { StationsService } from '../../../services/stations/stations.service';
import { Station } from '../../../classes/station.model';
import 'geolib';

@Component({
  selector: 'app-pollution',
 templateUrl: './pollution.component.html',
 styleUrls: ['./pollution.component.scss']
})
export class PollutionComponent implements OnInit {
private markers: Station[];

constructor(private stationsService: StationsService) { }

ngOnInit() {
    this.stationsService.GetAll().subscribe(
        res => {
            for (let i of res) {
                var t = geolib.computeDestinationPoint({ latitude: i.Lat, longitude: i.Long }, 1234, 90);
            }
            this.markers = res;
        }
    );
  }
} 

geloibs .d.ts 文件如下

declare namespace geolib {    
.....
}
declare module "geolib" {
 export = geolib;
}

工具没有抱怨,一切都编译了,但是当我运行它时,geolib 没有定义。

【问题讨论】:

  • 你试过 import * as geolib from 'geolib';
  • 成功了!谢谢!提交作为答案,我会这样标记。
  • 完成,很高兴为您提供帮助

标签: angular npm node-modules angular5


【解决方案1】:

使用此语法导入以这种方式构建的模块:

import * as geolib from 'geolib';

【讨论】:

    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 2017-10-22
    • 2019-02-08
    • 1970-01-01
    • 2019-10-30
    • 2017-10-29
    • 2023-03-21
    • 2017-05-29
    相关资源
    最近更新 更多