我正在尝试在新的 ionic 4 项目中使用 cordova 插件,但我总是遇到有关 cordova 的错误。该插件已正确安装并显示在插件文件夹中。
Error
本机:尝试调用 SplashScreen.hide,但 Cordova 不可用。确保包含 cordova.js 或在设备/模拟器中运行
主页.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>
主页.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));
}
});
}
}