【问题标题】:Ionic 4: Cordova is not available. Make sure to include cordova.js or run in a device/simulatorIonic 4:Cordova 不可用。确保包含 cordova.js 或在设备/模拟器中运行
【发布时间】:2019-05-03 16:40:31
【问题描述】:

我正在尝试在一个新的 ionic 4 项目中使用 cordova 插件,但我总是遇到有关 cordova 的错误。该插件已正确安装并显示在插件文件夹中。

错误

Native:尝试调用 SplashScreen.hide,但 Cordova 不可用。确保包含 cordova.js 或在设备/模拟器中运行

home.page.html

<ion-header>
  <ion-toolbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>
  <ion-button expand="full" (click)="openLocalPdf()">Open Local PDF</ion-button>
  <ion-button expand="full" (click)="downloadAndOpenPdf()">Download and open PDF</ion-button>
</ion-content>

home.page.ts

import { Platform } from '@ionic/angular';
import { File } from '@ionic-native/File/ngx';
import { Component } from '@angular/core';
import { FileOpener } from '@ionic-native/file-opener/ngx';
import { DocumentViewer, DocumentViewerOptions } from '@ionic-native/document-viewer/ngx';
import { FileTransfer } from '@ionic-native/file-transfer/ngx';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  constructor(private platform: Platform, private file: File, private ft: FileTransfer,
              private fileOpener: FileOpener, private document: DocumentViewer, ) {

  }
  openLocalPdf() {
        const filePath = this.file.applicationDirectory + 'www/assets';

        if (this.platform.is('android')) {
      const fakeName = Date.now();
      this.file.copyFile(filePath, '5-tools.pdf', this.file.dataDirectory, `${fakeName}.pdf`).then(result => {
        this.fileOpener.open(result.nativeURL, 'application/pdf')
          .then(() => console.log('File is opened'))
          .catch(e => console.log('Error opening file', e));
      });
    } else {
      // Use Document viewer for iOS for a better UI
      const options: DocumentViewerOptions = {
        title: 'My PDF'
      };
      this.document.viewDocument(`${filePath}/5-tools.pdf`, 'application/pdf', options);
    }
  }

  downloadAndOpenPdf() {
    const downloadUrl = 'https://devdactic.com/html/5-simple-hacks-LBT.pdf';
    const path = this.file.dataDirectory;
    const transfer = this.ft.create();

    transfer.download(downloadUrl, path + 'myfile.pdf').then(entry => {
      const url = entry.toURL();

      if (this.platform.is('ios')) {
        this.document.viewDocument(url, 'application/pdf', {});
      } else {
        this.fileOpener.open(url, 'application/pdf')
          .then(() => console.log('File is opened'))
          .catch(e => console.log('Error opening file', e));
      }
    });
  }
}

【问题讨论】:

  • 你是在使用 ionic serve 运行这个吗?
  • 尝试使用“ionic cordova run browser”时出现此错误“SitewaertsDocumentViewer.canViewDocument() 中的错误:缺少命令错误”
  • 你在使用任何原生插件吗?
  • 是的,它看起来像。这就是为什么我建议使用浏览器的第二个选项不会给你任何离子原生插件的原因。如果是这种情况,您应该使用设备或模拟器来测试您的应用

标签: ionic4


【解决方案1】:

当您使用 ionic serve 时,您将应用作为网站运行。所以科尔多瓦将不可用。从而导致你得到的错误

您可以通过多种方式绕过它。

首先,您可以使用以下命令在带有cordova的浏览器上运行它

ionic cordova platform add browser
ionic cordova run browser 

在浏览器上运行

其次,您可以通过发布本机(或使用模拟器)进行测试

ionic cordova platform add <ios/android>
ionic cordova run <android/ios> <--device>

--device 如果您使用的是物理设备,如果您打算使用模拟器。 当然这需要 JAVA SDK、ANDROID SDK 和 Gradle

从长远来看,我建议您稍后再使用,因为您已经在本机设备上测试了该应用程序

【讨论】:

  • 这在我尝试在浏览器上运行的 Macbook 上对我不起作用。
  • 它在浏览器中工作,也可以在浏览器中运行cordova文件浏览,我可以浏览我的文件,相机也在这里工作,但它需要更多的时间,我怎样才能更快地重新启动它?
  • 我收到此错误:在您的 PATH 上找不到本机运行。请在全局安装它没有帮助
猜你喜欢
  • 2017-04-10
  • 2018-08-20
  • 2015-07-31
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
  • 2019-12-16
  • 2020-07-15
  • 1970-01-01
相关资源
最近更新 更多