离子4:Cordova不可用.确保包含cordova.js或在设备/模拟器中运行 [英] Ionic 4: Cordova is not available. Make sure to include cordova.js or run in a device/simulator

查看:492
本文介绍了离子4:Cordova不可用.确保包含cordova.js或在设备/模拟器中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use the cordova plugin in a new ionic 4 project but I always run into errors regarding cordova. The plugin is properly installed and shows up in the plugin folder.

错误

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

Native: tried calling SplashScreen.hide, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator

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服务时,您可以将应用程序作为网站运行.因此,cordova将不可用.从而导致您得到错误

When you are using ionic serve you run your app as a website. So cordova won't be available. Thus leading to the error you are getting

您可以通过多种方式绕过此操作.

You can bypass this in several ways.

首先,您可以使用以下代码通过cordova在浏览器上运行

First you could use the following to run it on the browser with 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

--device if you are using a physical device or not if you intend to use an emulator. Of course this will require the JAVA SDK, ANDROID SDK and Gradle

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

In the long run I would recommend to go with later since you have test the app on a native device anyway

这篇关于离子4:Cordova不可用.确保包含cordova.js或在设备/模拟器中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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