【问题标题】:Can I add attributes to 'window' object in Angular 7? [duplicate]我可以在 Angular 7 中向“窗口”对象添加属性吗? [复制]
【发布时间】:2020-02-07 21:02:11
【问题描述】:

我可以向 Angular 中的“窗口”对象添加任何属性吗?比如:

window.attr = "my_value"

我尝试使用对象 windows bat 有错误:

“窗口”类型上不存在属性“attr”。

【问题讨论】:

  • 实现方法 1. window['attr'] 2. declare var window: any; 在文件顶部

标签: angular typescript


【解决方案1】:

尝试将窗口投射到any 喜欢

(window as any).attr = "my_value"

或者在导入之后在组件顶部定义类型

declare var window: any;

【讨论】:

    【解决方案2】:

    试试这样:

     Window["attr"] = "my_value";
    

    【讨论】: