angular - node_modules/bingmaps/types/MicrosoftMaps/Microsoft.Maps.d.ts' 不是模块
问题描述
我正在使用 angular 7 (cli) 的官方 npm bingmaps。
我添加了 npm 文档中提到的完成配置,
我目前正在加载基本的 bing 地图。
在我的 component.ts 文件中,我添加了以下行,因为如果不使用此行,编译器会给出错误(未定义“Microsoft”)
import { } from '../../../../../../node_modules/bingmaps/types/MicrosoftMaps/Microsoft.Maps.All';
现在,当我编译代码时,我收到另一个错误,因为Microsoft.Maps.All 不是模块。
对此有任何想法吗?这是与角度 CLI 相关的问题吗?我也提到了下面的链接,但没有得到他们想说的。
git链接
解决方案
您正在尝试导入模块,请考虑导入库:
import 'bingmaps';
超越此编译错误的另一个选择是 通过文件包含对@types/bingmaps
包的依赖:tsconfig.json
"compilerOptions": {
//..
"types": ["bingmaps"]
}
这是一个关于如何在 Angular2+ 应用程序中使用bingmaps
包的示例:
map.component.ts
文件:
/// <reference types="@types/bingmaps" />
import { Component, OnInit } from "@angular/core";
import { BingMapsAPILoader } from "./BingMapsAPILoader";
@Component({
selector: "app-map",
templateUrl: "./map.component.html"
})
export class AppComponent {
constructor (loader: BingMapsAPILoader) {
loader.load("bingAPIReady").then(() => this.initMap());
}
protected initMap() {
const options: Microsoft.Maps.IMapLoadOptions = {
center: new Microsoft.Maps.Location(47.60357, -122.32945),
credentials:
"--your Bing Maps API Key goes here--"
};
const map = new Microsoft.Maps.Map(document.getElementById("map"), options);
}
}
map.component.html 文件
<div id="map" style="width: 600px; height: 400px;"></div>
BingMapsAPILoader
加载 Bing 地图库的服务在哪里:
import { Injectable } from "@angular/core";
@Injectable()
export class BingMapsAPILoader {
private getNativeWindow(): any {
return window;
}
private getNativeDocument(): any {
return document;
}
public load(callbackName: string): Promise<void> {
return new Promise<void>((resolve: Function, reject: Function) => {
const script = this.getNativeDocument().createElement('script');
script.type = 'text/javascript';
script.async = true;
script.defer = true;
script.src = 'https://www.bing.com/api/maps/mapcontrol?callback=bingAPIReady';
this.getNativeWindow()[callbackName] = () => {
resolve();
};
script.onerror = (error: Event) => {
reject(error);
};
this.getNativeDocument().body.appendChild(script);
});
}
}
这是一个演示供您参考
推荐阅读
- c# - blazor 的 CSS 隔离问题
- indexing - Mapserver:使用 ogrtindex 的矢量 netCDF 数据的 Tileindex
- node.js - 如何删除合并声明中的单个声明必须全部导出或全部 local.ts 错误?
- html - 如何以相同的顺序设置标签?
- python - 如何使用自定义函数创建等高线图?
- openedge - Progress OpenEdge .p 文件调用 java 类的互操作性是什么,因为它们都可用于 Eclipse?
- python - pytorch 中的 output.data 是什么意思?
- hash - 如果你有一个来自 [n]->[m] 的成对独立哈希函数,如果数组中有 b 个单元格填充,碰撞的概率是多少?
- reactjs - 使用 Chakra Ui 时避免使用魔法弦
- sql - 露西
执行“updateDatasource”操作时未正确存储 connectionString 属性