angular - TS2564 属性 mapViewEl 没有初始化程序

标签 angular typescript arcmap

我正在努力为 Angular (v4) 获取 ArcMap Javascript 以在 Angular (v10) 项目中显示 map 。我发现的所有代码示例( hereESRI Offical here 和 stackblitz here )都非常相似。 viewChild 是这样创建的:

export class EsriMapComponent implements OnInit, OnDestroy {
@ViewChild("mapViewNode", { static: true }) private mapViewEl: ElementRef;
view: any;
constructor() { }
但是我的 IDE 通知我 TS2564 Property mapViewEl has no initializer and is not definitely assigned in the constructor .
我可以将此行更改为:
@ViewChild("mapViewNode", { static: true }) private mapViewEl!: ElementRef;
map 已绘制,但我不喜欢删除 Typescript 保护,尤其是因为我在网上找到的任何工作示例中都没有这样做。
esri-map.component.html 非常简单:<div #mapViewNode></div> .
如何修复 TS2564 错误?

最佳答案

我的建议是使用可选运算符( ? )而不是非空断言( ! )。因为属性(property)实际上是undefined直到组件被渲染,所以,最终你需要潜在地管理 undefined属性中的值。此外,如果 @ViewChild('compName') 中的组件名称不正确的属性将是 undefined所以如果你把它标记为可选 听起来比 not-null 更真实选择。

@ViewChild("mapViewNode", { static: true }) private mapViewEl?: ElementRef;

关于angular - TS2564 属性 mapViewEl 没有初始化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63783315/

相关文章:

node.js - typescript 异步生成器,用于套接字

python - 如何使用 python 将 .dbf 转换为 shapefile

*NgIf 完成后 Angular 4 运行代码

angular - 在 CanActivate 期间显示加载指示器

css - 如何在 Typescript 的文本框上创建 'glow' 效果?

python - 从光栅图像创建 numPy 数组

python - 如何在 shapefile (arcmap) 的属性表中将 'ß' 替换为 'ss'? ASCII 错误

Angular2 @Input 和生命周期钩子(Hook)

angular - Ionic 2 "Can' t解析所有参数”导入组件时

angular - 如何在 typescript 中使用 formData.append 作为数组