繁体   English   中英

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

[英]Declare ambient variable if not null in Typescript

我在以下行中declare var Notification: any;

在大多数情况下,它运行良好。 但是在iOS上,我看到以下异常ReferenceError: Can't find variable: Notification iOS似乎不支持Notification ,有没有一种方法可以检查它是否为null,以及是否不声明它。 像这样-

if (Notification)
  declare var Notification: any;

声明它(编译时间)

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

测试(运行时)

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

环境声明(例如declare var仅在编译期间有用。 它们在执行期间不可用。

您遇到的消息错误意味着未定义Notification 使用declare var不会改变这一事实。 您在代码中错过了变量Notification的实际实例化/定义。

暂无
暂无

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

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