【问题标题】:TypeScript error: Property 'app' does not exist on type 'Navigator'TypeScript 错误:“导航器”类型上不存在属性“应用程序”
【发布时间】:2017-02-26 05:37:43
【问题描述】:

我不断收到此错误:

> TypeScript error: Property 'app' does not exist on type 'Navigator'

使用此代码时:

navigator.app.exitApp();

我有以下插件:

> <plugin name="cordova-plugin-device" spec="~1.1.2"/>
> <plugin name="cordova-plugin-console" spec="~1.0.3"/>
> <plugin name="cordova-plugin-whitelist" spec="~1.2.2"/>
> <plugin name="cordova-plugin-splashscreen" spec="~3.2.2"/>
> <plugin name="cordova-plugin-statusbar" spec="~2.1.3"/>
> <plugin name="ionic-plugin-keyboard" spec="~2.2.1"/>

我的代码可能有什么问题?

【问题讨论】:

标签: angular typescript ionic2


【解决方案1】:

只需将应用属性添加到导航器界面

interface Navigator {
    app: {
        exitApp: () => any; // Or whatever is the type of the exitApp function
    }
}

【讨论】:

  • 其实我想通过设备后退按钮确认应用退出,并被告知使用navigator.app.exitApp();在用户确认后实际退出。
  • 所以,我不知道exitApp函数的内容是什么。
  • 我为exitApp 提供了一个非常通用的类型,它应该适用于您的代码
  • 我遇到了同样的问题,我无法完全理解我必须如何或在哪里使用此代码。可以请人给我一个提示吗?
【解决方案2】:

1.注入全局

declare global {
  interface Navigator {
    app: any;
  }
}
navigator.clipboard; // origin property
navigator.app.exitApp(); // inject property 

2。和任何人一样

(navigator as any).app.exitApp();

这应该可行。

3.声明一个新接口

如果您已导入 dom 库,请检查 typescript/lib/lib.dom.d.ts。

interface NavigatorCordova extends Navigator {
    app: {
        exitApp: () => any; // Or whatever is the type of the exitApp function
    }
}
(navigator as NavigatorCordova).app.exitApp();

就像这个答案。 https://stackoverflow.com/a/40083037/9515185

【讨论】:

    【解决方案3】:

    你需要有相关的类型。

    1) 安装类型包

    npm install typings --global
    

    2)安装包的相关类型,如果不确定插件类型名称,也可以搜索。

    typings search cordova-plugin-device-orientation
    
    typings install dt~cordova-plugin-device-orientation --global --save
    

    你很高兴

    【讨论】:

      猜你喜欢
      • 2018-01-13
      • 2019-09-07
      • 2019-09-01
      • 2016-11-17
      • 1970-01-01
      • 2023-02-21
      • 2019-04-12
      • 2020-07-09
      • 2019-10-20
      相关资源
      最近更新 更多