gpt4 book ai didi

ionic4 - Ionic 4 : Cordova is not available. 确保包含 cordova.js 或在设备/模拟器中运行

转载 作者:行者123 更新时间:2023-12-02 22:00:58 25 4
gpt4 key购买 nike

我正在尝试在新的 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 服务时,您将应用程序作为网站运行。所以 Cordova 将不可用。从而导致您收到错误

您可以通过多种方式绕过此问题。

首先,您可以使用以下命令在带有 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

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

关于ionic4 - Ionic 4 : Cordova is not available. 确保包含 cordova.js 或在设备/模拟器中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55965710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com