简体   繁体   English

声明环境变量(如果Typescript中不为null)

[英]Declare ambient variable if not null in Typescript

I have the following line declare var Notification: any; 我在以下行中declare var Notification: any; .

It's working fine, for the most part. 在大多数情况下,它运行良好。 But on iOS I see the following exception - ReferenceError: Can't find variable: Notification . 但是在iOS上,我看到以下异常ReferenceError: Can't find variable: Notification iOS doesn't seem to support Notification , is there a way to check if is it null, and if it is do not declare it. iOS似乎不支持Notification ,有没有一种方法可以检查它是否为null,以及是否不声明它。 Something like this - 像这样-

if (Notification)
  declare var Notification: any;

Declare it (compile time) 声明它(编译时间)

declare var Notification: any;  // <= this is for the compiler only

Test it (runtime) 测试(运行时)

if (typeof Notification !== 'undefined') {   
   //non IOS stuff
}

Ambient declarations such as declare var are only useful during compilation time. 环境声明(例如declare var仅在编译期间有用。 They are not available during execution time. 它们在执行期间不可用。

The message error you have means that Notification are not defined. 您遇到的消息错误意味着未定义Notification Using declare var will not change that fact. 使用declare var不会改变这一事实。 You miss in your code an actual instanciation/definition of your variable Notification . 您在代码中错过了变量Notification的实际实例化/定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM