javascript - 如果 Typescript 中不为 null,则声明环境变量

标签 javascript typescript

我有以下行declare var notification: any;

在大多数情况下,它运行良好。但在 iOS 上,我看到以下异常 - ReferenceError: Can't findvariable: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
}

关于javascript - 如果 Typescript 中不为 null,则声明环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38661097/

相关文章:

angular - 如何使用 Protractor typescript 使用 _ngcontent 和 span buttontext 单击元素

javascript - 用于测试日期格式的正则表达式

javascript - Sweetalert 中的服务无法运行

javascript - 按日期分组的问题

javascript - 在 Objection.js 中以多对多方式插入额外字段

javascript - 如何防止/延迟一堆 Bootstrap 模块立即加载它们的内容以防止站点加载时间变慢?

javascript - 尝试使用 'for' 循环构建数组

JavaScript,将对象转换为数组

javascript - 离线观看带字幕的 HTML5 视频

angular - 如果条件在 RxJS 中为真,我可以跳过所有后续运算符链吗