2016-07-29 50 views
0

我有以下行declare var Notification: any;在Typescript中声明环境变量如果不为空

它的工作很好,大部分。但在iOS上,我看到以下例外情况 - ReferenceError: Can't find variable: Notification。 iOS似乎不支持Notification,有没有办法检查它是否为空,如果它不声明它。类似的东西 -

if (Notification) 
    declare var Notification: any; 

回答

2

声明它(编译时间)

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

测试它(运行时)

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

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

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