TypeScript错误:"导航器"类型中不存在属性"app"

Awe*_*eda 4 typescript ionic2 angular

我一直收到这个错误:

> TypeScript error: Property 'app' does not exist on type 'Navigator'
Run Code Online (Sandbox Code Playgroud)

使用此代码时:

navigator.app.exitApp();
Run Code Online (Sandbox Code Playgroud)

我有以下插件:

> <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"/>
Run Code Online (Sandbox Code Playgroud)

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

Nah*_*nde 6

只需将一个app属性添加到Navigator界面即可

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


Pau*_*Han 6

1. 注入全局

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

2. 随便

(navigator as any).app.exitApp();
Run Code Online (Sandbox Code Playgroud)

这应该有效。

3.声明一个新的接口

如果您导入了 dom lib,请检查 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();
Run Code Online (Sandbox Code Playgroud)

就像这个答案一样。/sf/answers/2805812621/