import {Component} from '@angular/core';
import {NavController, Platform, AlertController} from 'ionic-angular';
import {Transfer, TransferObject} from '@ionic-native/transfer';
import {File} from '@ionic-native/file';
@Component({
selector: 'page-about',
templateUrl: 'about.html',
providers: [Transfer, TransferObject, File]
})
export class AboutPage {
storageDirectory: string = '';
constructor(public navCtrl: NavController, public platform: Platform, private transfer: Transfer, private file: File, public alertCtrl: AlertController) {
this.platform.ready().then(() => {
// make sure this is on a device, not an emulation (e.g. chrome tools device mode)
if(!this.platform.is('cordova')) {
return false;
}
if (this.platform.is('ios')) {
this.storageDirectory = cordova.file.documentsDirectory;
}
else if(this.platform.is('android')) {
this.storageDirectory = cordova.file.externalDataDirectory;
console.log(this.storageDirectory);
}
else {
// exit otherwise, but you could add further types here e.g. Windows
return false;
}
});
}
downloadImage() {
this.platform.ready().then(() => {
const fileTransfer: TransferObject = this.transfer.create();
const imageLocation = 'http://html5demos.com/assets/dizzy.mp4';
fileTransfer.download(imageLocation, this.storageDirectory + 'dizzy.mp4').then((entry) => {
const alertSuccess = this.alertCtrl.create({
title: `Download Succeeded!`,
subTitle: `successfully downloaded to: ${entry.toURL()}`,
buttons: ['Ok']
});
alertSuccess.present();
}, (error) => {
const alertFailure = this.alertCtrl.create({
title: `Download Failed!`,
subTitle: `was not downloaded. Error code: ${error}`,
buttons: ['Ok']
});
alertFailure.present();
});
});
}
}

I am getting the error attached in screenshot.我在离子2中运行项目构建时遇到错误,尽管我已经安装了' typings'使用以下命令
npm install -g typings typings, install dt~cordova --save --global
并尝试了每种可能的方法来删除此错误,检查所有cordova插件,如文件,文件传输但仍然无法解决错误。
任何人都可以找到它。
这里附有代码,我也不知道我哪里出错......
答案 0 :(得分:9)
我已经编辑了你的代码,添加了declare let cordova: any;
这暴露了cordova api以供使用。希望这有帮助。
import {Component} from '@angular/core';
import {NavController, Platform, AlertController} from 'ionic-angular';
import {Transfer, TransferObject} from '@ionic-native/transfer';
import {File} from '@ionic-native/file';
declare let cordova: any;
@Component({
selector: 'page-about',
templateUrl: 'about.html',
providers: [Transfer, TransferObject, File]
})
export class AboutPage {
storageDirectory: string = '';
constructor(public navCtrl: NavController, public platform: Platform, private transfer: Transfer, private file: File, public alertCtrl: AlertController) {
this.platform.ready().then(() => {
// make sure this is on a device, not an emulation (e.g. chrome tools device mode)
if(!this.platform.is('cordova')) {
return false;
}
if (this.platform.is('ios')) {
this.storageDirectory = cordova.file.documentsDirectory;
}
else if(this.platform.is('android')) {
this.storageDirectory = cordova.file.externalDataDirectory;
console.log(this.storageDirectory);
}
else {
// exit otherwise, but you could add further types here e.g. Windows
return false;
}
});
}
downloadImage() {
this.platform.ready().then(() => {
const fileTransfer: TransferObject = this.transfer.create();
const imageLocation = 'http://html5demos.com/assets/dizzy.mp4';
fileTransfer.download(imageLocation, this.storageDirectory + 'dizzy.mp4').then((entry) => {
const alertSuccess = this.alertCtrl.create({
title: `Download Succeeded!`,
subTitle: `successfully downloaded to: ${entry.toURL()}`,
buttons: ['Ok']
});
alertSuccess.present();
}, (error) => {
const alertFailure = this.alertCtrl.create({
title: `Download Failed!`,
subTitle: `was not downloaded. Error code: ${error}`,
buttons: ['Ok']
});
alertFailure.present();
});
});
}
}

答案 1 :(得分:0)
您必须在cordova
文件中声明src/declarations.d.ts
命名空间,以便typescript转换器可以理解cordova被声明并引用到Object,但在您的情况下,如果您想使用插件,则更好使用ionic-native如果插件没有列在那里,请声明插件的命名空间并使用它。