未捕获(承诺):Ionic 2 中的 cordova_not_available [英] Uncaught (in promise): cordova_not_available in Ionic 2

查看:20
本文介绍了未捕获(承诺):Ionic 2 中的 cordova_not_available的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 ionic serve -l 命令运行 Ionic 应用程序时,收到以下错误消息:

When I'm running my Ionic app with ionic serve -l command, get following error message:

运行时错误

未捕获(承诺):cordova_not_available

Uncaught (in promise): cordova_not_available

堆栈

Error: Uncaught (in promise): cordova_not_available
at v (http://localhost:8100/build/polyfills.js:3:4864)
at s (http://localhost:8100/build/polyfills.js:3:4289)
at s (http://localhost:8100/build/polyfills.js:3:4112)
at http://localhost:8100/build/polyfills.js:3:4652
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:10284)
at Object.onInvokeTask (http://localhost:8100/build/main.js:38692:37)
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:10220)
at e.runTask (http://localhost:8100/build/polyfills.js:3:7637)
at i (http://localhost:8100/build/polyfills.js:3:3707)
at HTMLDocument.invoke (http://localhost:8100/build/polyfills.js:3:11437)

其他细节

Ionic Framework: 2.2.0
Ionic Native: 2.8.1
Ionic App Scripts: 1.1.4
Angular Core: 2.4.8
Angular Compiler CLI: 2.4.8
Node: 6.9.2
OS Platform: Windows 10
Navigator Platform: Win32
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36

我安装 HotSpot 插件,使用以下命令:

I Install HotSpot plugin, with following command :

ionic plugin add cordova-plugin-hotspot --save

cordova plugin add cordova-plugin-hotspot --save

用法 app.component.ts

Usage app.component.ts

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar, Splashscreen,Hotspot,Network } from 'ionic-native';
import { TabsPage } from '../pages/tabs/tabs';


@Component({
 templateUrl: 'app.html'
})

export class MyApp {
rootPage = TabsPage;

 constructor(platform: Platform) {

  platform.ready().then(() => {
     StatusBar.styleDefault();
     Splashscreen.hide();
     Hotspot.scanWifi().then((networks: Array<Network>) => {
     console.log(networks);

 });
})
   }
  }

另外,我看到了类似的问题,但他们没有解决不了问题.

Also, I saw similar questions but they didn't solve the problem.

推荐答案

您在浏览器中进行测试时正在访问本机插件.为了使插件工作,您应该使用真实设备进行测试.

You are accessing native plugins while testing in the browser. In order to make plugins work, you should use a real device to test.

为了使您的代码在浏览器中可测试(或者实际上在浏览器中测试时不会中断),您应该使用 if 语句检查 Cordova 是否可用:

In order to make your code testable in browser (or actually don't break when testing in browser) you should have an if-statement checking if Cordova is available :

  if (this.platform.is('cordova')) {
    // You're on a device, call the native plugins. Example: 
    //
    // var url: string = '';
    // 
    // Camera.getPicture().then((fileUri) => url = fileUri);
  } else {
    // You're testing in browser, do nothing or mock the plugins' behaviour.
    //
    // var url: string = 'assets/mock-images/image.jpg';
  }

正如 Ricky Levi 在下面正确提到的,Ionic 支持 browser 平台.使用这个平台,大多数常见的插件都可以工作.请注意,某些插件不会,例如 Barcode-scanner 插件.因为它会提示您一个警报,要求您输入必须 scanned 的值.这将失去条形码扫描仪的整个用例.

As Ricky Levi correctly mentions here below, Ionic supports the browser platform. Using this platform, most common plugins are able to work. Note that some plugins would not, for example the Barcode-scanner plugin. As it will prompt you with an alert, asking for the value that has to be scanned. Which will lose the whole use-case of a Barcode Scanner.

这篇关于未捕获(承诺):Ionic 2 中的 cordova_not_available的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆