Я пытаюсь загрузить PDF-файл из папки с ресурсами в ionic или загрузить его. Я смотрел несколько руководств, но все они дают мне одну и ту же ошибку.
Ионный:
Кордова:
Система:
Код :
import { Component } from '@angular/core';
import { DocumentViewer, DocumentViewerOptions } from '@ionic-
native/document-viewer/ngx';
import { FileOpener } from '@ionic-native/file-opener/ngx';
import { FileTransfer } from '@ionic-native/file-transfer/ngx';
import { File } from '@ionic-native/File/ngx';
import { NavController, Platform } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(
public navCtrl: NavController,
private file: File,
private ft: FileTransfer,
private document: DocumentViewer,
public platform: Platform
) {}
openLocalPdf() {
// let filePath = this.file.applicationDirectory + 'www/assets';
// if (this.platform.is('android') || this.platform.is('browser')) {
// let fakeName = Date.now();
// this.file.copyFile(filePath, 'Typescript_Jumpstart_Book_Udemy.pdf', this.file.dataDirectory, `${fakeName}.pdf`).then(result => {
// this.fileOpener.open(result.nativeURL, 'application/pdf')
// .then(() => console.info('File is opened'))
// .catch(e => console.info('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);
// }
console.info('Documents Pressed.....');
const options: DocumentViewerOptions = {
title: "My PDF"
}
this.document.viewDocument('assets/Typescript_Jumpstart_Book_Udemy.pdf', 'application/pdf', options)
console.info('Documents Pressed..... afterrr');
}
downloadAndOpenPdf() {
let path = null;
if (this.platform.is('ios')) {
path = this.file.documentsDirectory;
} else {
path = this.file.dataDirectory;
}
const transfer = this.ft.create();
transfer.download('https://devdactic.com/html/5-simple-hacks-LBT.pdf', path + 'MyFile.pdf').then(entry => {
let url = entry.toURL();
this.document.viewDocument(url, 'application/pdf', {});
// this.fileOpener.open(url, 'application/pdf')
// .then(() => console.info('File is opened'))
// .catch(e => console.info('Error opening file', e));
});
}
}
Ошибка:
ERROR TypeError: Object(...) is not a function
at DocumentViewer.viewDocument (index.js:29)
at HomePage.webpackJsonp.193.HomePage.openLocalPdf (home.ts:43)
at Object.eval [as handleEvent] (HomePage.html:10)
at handleEvent (core.js:13589)
at callWithDebugContext (core.js:15098)
at Object.debugHandleEvent [as handleEvent] (core.js:14685)
at dispatchEvent (core.js:10004)
at core.js:10629
at HTMLButtonElement.<anonymous> (platform-browser.js:2628)
at t.invokeTask (polyfills.js:3)





Вы используете Ionic 3 и используете версию плагина, поддерживаемую для Ionic 4. Используйте плагины, поддерживаемые для Ionic 3, изучив документ Ionic v3.
Ссылка: https://ionicframework.com/docs/v3/native/
Шаг 1
Удалите все свои родные плагины.
Шаг 2
Установите версию 4. Пример ниже
$ ionic cordova plugin add cordova-plugin-document-viewer
$ npm install --save @ionic-native/document-viewer@4
Шаг 3
Не добавляйте нгкс в конце импорта, это только для Angular 6.
import { DocumentViewer } from '@ionic-native/document-viewer';
Шаг 4:
Повторите шаги 2 и 3 для других собственных плагинов.
Я столкнулся с той же проблемой, мне нужно также понизить версию ionic cli?
Да, правильный ответ