我在angular5应用程序中使用arcgis-js-api类型。当我尝试导入多个组件中的类型时,我得到未捕获的引用错误:__esri未定义。 我已将arcgis-js-api包含在tsconfig.app.json文件中的types数组中。这是我的代码。
import { Component, OnInit, Input, ViewChild, ElementRef } from
'@angular/core';
import * as esriLoader from 'esri-loader';
import { environment } from '../../../environments/environment';
import Esri = __esri;
@Component({
selector: 'app-custom-widget',
templateUrl: './custom-widget.component.html',
styleUrls: ['./custom-widget.component.css']
})
export class CustomWidgetComponent implements OnInit {
private _mapView: Esri.MapView;
private _scriptOptions: any;
@ViewChild('customWidget') widgetElement: ElementRef;
@Input()
set mapView(mapView: Esri.MapView) {
if (!mapView) {
return;
}
this._mapView = mapView;
this.renderWidget();
}
get mapView(): Esri.MapView {
return this._mapView;
}
constructor() {
this._scriptOptions = {
url: environment.arcgisAPIVersion
};
}
ngOnInit() {
}
renderWidget(): void {
esriLoader.loadModules([
'esri/widgets/Widget'
], this._scriptOptions).then(([Widget]) => {
const widgetProperties: Esri.WidgetProperties = {
container: this.widgetElement.nativeElement
};
const widget: Esri.Widget = new Widget(widgetProperties);
this._mapView.ui.add(widget, 'bottom-right');
});
}
}
答案 0 :(得分:0)
我发现这个解决方案对我有用-
我创建了一个导入命名空间的模型文件,然后在我需要的任何地方使用这些类型(有效地避免了错误“ReferenceError: __esri is not defined, cannot use this namespace in multiple components”)。
示例:
在 esri.models.ts 文件中-
import esri = __esri;
export interface esriMapView extends esri.MapView {}
export interface esriLayerView extends esri.LayerView {}
在组件中-
import { esriMapView } from "../../modules/esri.models";
const mapView: esriMapView = new MapView({});