Typescript Error: Cannot find name 'cordova'
Typescript Error: Cannot find name 'cordova'
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.我在 运行 我的项目在 ionic 2 中构建时出现错误,尽管我已经使用以下命令安装了 'typings'
npm install -g typings typings, install dt~cordova --save --global
并尝试了所有可能的方法来消除此错误,检查了所有 cordova 插件,如文件、文件传输,但仍然没有解决错误。
谁能找找看
这里也附上代码,我不知道我哪里出错了..
你必须在 src/declarations.d.ts
文件中声明 cordova
命名空间,这样打字稿转译器才能理解 cordova 被声明并引用对象,但在你的情况下,如果你想使用插件,它最好使用 ionic-native,如果插件未在此处列出,请声明插件的命名空间并使用它。
我编辑了您的代码,添加了 declare let cordova: any;
这为 use.Hope 公开了 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();
});
});
}
}
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.我在 运行 我的项目在 ionic 2 中构建时出现错误,尽管我已经使用以下命令安装了 'typings'
npm install -g typings typings, install dt~cordova --save --global
并尝试了所有可能的方法来消除此错误,检查了所有 cordova 插件,如文件、文件传输,但仍然没有解决错误。
谁能找找看
这里也附上代码,我不知道我哪里出错了..
你必须在 src/declarations.d.ts
文件中声明 cordova
命名空间,这样打字稿转译器才能理解 cordova 被声明并引用对象,但在你的情况下,如果你想使用插件,它最好使用 ionic-native,如果插件未在此处列出,请声明插件的命名空间并使用它。
我编辑了您的代码,添加了 declare let cordova: any;
这为 use.Hope 公开了 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();
});
});
}
}